-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
229 lines (199 loc) · 8.69 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<html lang="en" ng-app="kafka">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Kafka</title>
<link href="//fonts.googleapis.com/css?family=Roboto+Mono|Work+Sans" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<style>
body{font-family:"Roboto Mono",monospace;font-size:30px;background-color: black;color: white;}
.line{margin:0;padding: 0;height: 1em;line-height: 1em;}
.line span{display: inline-block;min-width:0.6em;}
.line span.W{background-color: white;color: white;}
.line span.T{background-color: grey;color:grey;}
.line span.E{background-color: black;color:black;}
h1{float:right;margin-right: 30px;margin-top: 0.1em;width:300px;}
.legend{float:right;clear:both;margin-right: 30px;width:300px;}
.copy{position: absolute; bottom:1em;right:1em;font-family: "Work Sans";font-size: 12px;}
.copy a{color:#82e4e1; }
@media (max-width: 500px) {
body{font-size:14px;}
h1{float:none;}
.legend{float:none;}
.line {height: 1.3em;line-height: 1.3em;}
.line span{display: inline-block;min-width:2.7777%;max-width:2.7777%;}
.copy{position: relative; margin-top: 2em;right:initial;bottom: initial;}
}
</style>
<script>
var app = angular.module("kafka",[]);
app.controller('gameController', ['$scope', '$document', function($scope, $document){
var ctrl = this;
var A = [20, 36];
var CE = 1;
ctrl.score = 0;
ctrl.current_score = 0;
ctrl.init = function(){
// Réinitialisation du niveau
ctrl.tableau = [];
ctrl.player = [10,18];
ctrl.teleporters = [];
ctrl.ennemies = [];
ctrl.current_score = 0;
// Contruction du labyrinthe
for(x = 0 ; x < A[0] ; x++){
var line = [];
for(y = 0 ; y < A[1] ; y++){
if(x == 9 && y == A[1] -1){ line.push("E");
}else if(x == 9 && y == A[1] - 2){ line.push("");
}else if(
x == 0 || x == A[0]-1 || y == 0 || y == A[1]-1 // bordure
|| (x%2 == 0 && y%2 == 0) // une case sur deux colorée
){ line.push("W");
}else{
if(ctrl.tableau[x-1][y-1] == "W") line.push(""); // en bas à droite d'une pleine : une vide
else line.push((Math.random() < 0.333) ? "W" : ""); // 3/10 de mur sinon
}
}
ctrl.tableau.push(line);
}
// teleporteurs
for (var i = 0; i < 6; i++) {
do{
var tx = Math.round((Math.random()*8)+2)*2-1;
var ty = Math.round((Math.random()*10)+4)*2-1;
}while(ctrl.tableau[tx][ty] == "W")
ctrl.tableau[tx][ty] = "T";
ctrl.teleporters.push([tx,ty]);
}
//ennemies (plutôt sur la droite)
for (var i = 0; i < 15; i++) {
do{
var tx = Math.round((Math.random()*15)+2)-1;
var ty = Math.round((Math.random()*10)+25)-1;
}while(ctrl.tableau[tx][ty] == "W" || ctrl.tableau[tx][ty] == "T")
ctrl.tableau[tx][ty] = "#";
ctrl.ennemies.push([tx,ty]);
}
// player (plutôt sur la gauche)
do{
var tx = Math.round((Math.random()*8)+2)*2-1;
var ty = Math.round((Math.random()*10)+4)-1;
}while(ctrl.tableau[tx][ty] == "W" || ctrl.tableau[tx][ty] == "T" || ctrl.tableau[tx][ty] == "#")
ctrl.move(tx,ty)
}
ctrl.move = function(x,y, donotteleport){
if(ctrl.tableau[x][y] == "#"){
ctrl.score = Math.max(ctrl.score - 10, 0);
return false; //ennemy blocks
}
if(ctrl.tableau[x][y] == "W"){
ctrl.score = Math.max(ctrl.score - 10, 0);
return false; //wall blocks
}
if(ctrl.tableau[x][y] == "E"){
ctrl.exit(); return false; //exits labyrinth
}
ctrl.tableau[ctrl.player[0]][ctrl.player[1]] = "";
ctrl.player[0] = x; ctrl.player[1] = y;
ctrl.tableau[ctrl.player[0]][ctrl.player[1]] = "0";
// trigger teleporters (une seule fois : va au suivant)
for(var t in ctrl.teleporters){
if(ctrl.teleporters[t][0] == x && ctrl.teleporters[t][1] == y && !donotteleport){
var next_t = (parseInt(t)+1)%ctrl.teleporters.length;
ctrl.move(ctrl.teleporters[next_t][0],ctrl.teleporters[next_t][1], true);
break;
}
}
ctrl.current_score++;
ctrl.moveEnnemies();
}
ctrl.moveEnnemies = function(){
for(var e in ctrl.ennemies){
var dx = Math.sign(ctrl.player[0] - ctrl.ennemies[e][0]);
var dy = Math.sign(ctrl.player[1] - ctrl.ennemies[e][1]);
if( ctrl.tableau[ctrl.ennemies[e][0]+dx][ctrl.ennemies[e][1]+dy] == ""
||ctrl.tableau[ctrl.ennemies[e][0]+dx][ctrl.ennemies[e][1]+dy] == "T"
){
ctrl.tableau[ctrl.ennemies[e][0]][ctrl.ennemies[e][1]] = "";
ctrl.tableau[ctrl.ennemies[e][0]+dx][ctrl.ennemies[e][1]+dy] = "#";
ctrl.ennemies[e] = [ctrl.ennemies[e][0]+dx, ctrl.ennemies[e][1]+dy]
}
}
}
ctrl.left = function(){ ctrl.move(ctrl.player[0],ctrl.player[1]-1) }
ctrl.right = function(){ctrl.move(ctrl.player[0],ctrl.player[1]+1)}
ctrl.up = function(){ctrl.move(ctrl.player[0]-1,ctrl.player[1])}
ctrl.down = function(){ctrl.move(ctrl.player[0]+1,ctrl.player[1])}
ctrl.destroy = function(){
for(x = -1 ; x <= 1 ; x ++){
for(y = -1 ; y <= 1 ; y ++){
if(y == 0 && x == 0) continue;
if( ctrl.player[0]+x == 0 || ctrl.player[1]+y == 0
|| ctrl.player[0]+x == A[0]-1 || ctrl.player[1]+y == A[1]-1
|| ctrl.tableau[ctrl.player[0]+x][ctrl.player[1]+y] != "W"
)continue
ctrl.tableau[ctrl.player[0]+x][ctrl.player[1]+y] = "";
}
}
ctrl.score = Math.max(ctrl.score - ctrl.current_score, 0);
}
ctrl.renonce = function(){
ctrl.score = Math.max(ctrl.score - 100 + ctrl.current_score, 0);
ctrl.init()
}
ctrl.exit = function(){
ctrl.score += 2*ctrl.current_score;
if(ctrl.score >= 1000){
ctrl.tableau[9].push("Vous êtes libre");
}else{
ctrl.init()
}
}
$document.bind("keydown", function($event) {
if ($event.keyCode == 38){
ctrl.up(); $event.preventDefault();
}else if ($event.keyCode == 39){
ctrl.right();$event.preventDefault();
}else if ($event.keyCode == 40){
ctrl.down();$event.preventDefault();
}else if ($event.keyCode == 37){
ctrl.left();$event.preventDefault();
}else if ($event.keyCode == 68){ //t
ctrl.destroy();$event.preventDefault();
}else if ($event.keyCode == 82){ //r
ctrl.renonce();$event.preventDefault();
}
$scope.$apply();
});
ctrl.init();
}])
</script>
</head>
<body ng-controller="gameController as ctrl">
<h1>Kafka</h1>
<div class="legend">
<div class="line">
<span>0</span> : Vous
</div>
<div class="line">
<span class="T">T</span> : Téléporteur
</div>
<div class="line">
<span class="W">W</span> : Mur
</div>
<div class="line">
<span>#</span> : Ennemi
</div>
</div>
<div class="line" ng-repeat="line in ctrl.tableau track by $index">
<span ng-repeat="chr in line track by $index" ng-class="chr">{{chr}}</span>
</div>
<div class="score">
Score = {{ctrl.score}}/1000<br />
←↑→↓ - [R]enoncer - [D]étruire
</div>
<div class="copy"><a href="http://troisyaourts.com" target="_blank">©TroisYaourts 2017</a> - cette version <a href="http://twitter.com/smwhr/" target="_blank">@smwhr</a> d'après un code source original en Basic publié en juin 1988 dans <a href="http://www.abandonware-magazines.org/affiche_mag.php?mag=185&num=4018&album=oui" target="_blank">Jeux & Stratégie Nº51 (pages 88-89)</a>.</div>
</body>
</html>