-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
252 lines (212 loc) · 6.33 KB
/
main.js
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
var testingMode = false;
var collisionBox = false;
//stage variables
var stageWidth = 832; //26*32 = 832
var stageHeight = 512; //16*32 = 512
var tileSize = 32;
//global state and variables
var level;
var stage;
var points;
var killedIndians;
var indiansToKill;
var pointsHUD = document.getElementById('points');
var lifeHUD = document.getElementById('life');
var finalText = null; //todo remove this global var
var marginTop = 25;
var player = null;
var indians = new Array();
var boundaryLeft;
var boundaryRight;
var dialog = null;
var nextMsgTime;
function redefineCraftyKeys() {
// at some point of browsers evolution, Crafty's code for handling key presses has stopped working.
// this function redefines the key codes used on the game, based on current browsers (as of the year 2020)
Crafty.keys.SPACE = ' ';
Crafty.keys.ENTER = 'Enter';
Crafty.keys.UP_ARROW = 'ArrowUp';
Crafty.keys.DOWN_ARROW = 'ArrowDown';
Crafty.keys.LEFT_ARROW = 'ArrowLeft';
Crafty.keys.RIGHT_ARROW = 'ArrowRight';
Crafty.keys.NUMPAD_0 = '0';
Crafty.keys.W = 'W';
Crafty.keys.w = 'w';
Crafty.keys['5'] = '5';
}
function reset()
{
Crafty.viewport.x = 0;
level = 0;
stage = 0;
points = 0;
killedIndians = 0;
indiansToKill = 20;
dialog = null;
nextMsgTime = -1;
pointsHUD.innerHTML = 0;
lifeHUD.innerHTML = 5;
boundaryLeft = -30;
boundaryRight = stageWidth;
}
function generateLevel()
{
//generate floor to prevent the characters to fall
var floorTile = Crafty.e('2D, DOM, Collision, floor')
.attr({ x: -100, y: 13*tileSize+50, w:map1.size + 2000, h:3, z:10 });
//generate trees
createTree(map1.trees[0].x, map1.trees[0].y, map1.trees[0].type, i)
.attr('isFirst', true);
for(var i=1;i<map1.trees.length;i++)
{
createTree(map1.trees[i].x, map1.trees[i].y, map1.trees[i].type, i);
};
};
window.onload = function ()
{
Crafty.init(stageWidth, stageHeight);
setupImages();
initializeGameComponents();
initializeEnemyComponents();
Crafty.viewport.clampToEntities = false;
redefineCraftyKeys();
//todo check if the audio files can be loaded with the load function
//using names as .add, instead of using the files
var sounds =
{
'chop': 'assets/audio/chop.ogg',
'playerHurt': 'assets/audio/6.ogg',
'hurt': 'assets/audio/randomize12.ogg',
'treeFall': 'assets/audio/explosion3.ogg',
'go': 'assets/audio/8repeat.ogg',
'music': 'assets/audio/first.ogg'
};
var assets = new Array();
for (image in images) assets.push(images[image]);
for (sound in sounds) assets.push(sounds[sound]);
Crafty.scene('loading', function ()
{
Crafty.background('rgb(30,30,30)');
var loadingText = Crafty.e('2D, DOM, Text')
.attr({ w: 480, h: 20, x: 0, y: 20 })
.text('Loading...')
.css({ 'text-align': 'center' });
Crafty.load(assets, function ()
{
setupImages();
Crafty.audio.add("chop", "assets/audio/chop.ogg");
Crafty.audio.add("playerHurt", "assets/audio/6.ogg");
Crafty.audio.add("hurt", "assets/audio/randomize12.ogg");
Crafty.audio.add("treeFall", "assets/audio/explosion3.ogg");
Crafty.audio.add("go", "assets/audio/8repeat.ogg");
Crafty.audio.add("music", "assets/audio/first.ogg");
loadingText.destroy();
Crafty.scene('title');
},
function (progress)
{
console.log(progress.percent);
});
});
Crafty.scene('title', function ()
{
Crafty.background('url("assets/images/bg.png")');
reset(); console.log('here');
Crafty.e('2D, DOM, title')
.attr({ x: 0, y: 20, z:10 })
;
Crafty.e('2D, DOM, Text, Keyboard')
.attr({ w: 300, h: 50, x: 270, y: 385, z:100 })
.text('(press SPACE to play)')
.css({ 'text-align': 'center' })
.css({ 'color': 'white' })
.bind('KeyDown', function(e)
{
if(e.key == Crafty.keys['SPACE'] || e.key == Crafty.keys['ENTER'])
{
Crafty.scene('level1');
}
});
});
Crafty.scene('level1', function ()
{
Crafty.background('url("assets/images/bg.png")');
Crafty.audio.play("music", -1, 0.6);
generateLevel();
createPlayer(3,11);
});
Crafty.scene('loading');
}
function createPlayer(x, y)
{
player = Crafty.e('2D, DOM, joe, Tween, Twoway, Collision, Gravity, Ape, Player, AxeAttacker, LevelManager')
.attr({ x: x * 32, y: y * 32, z:1000 })
.twoway(3, 5)
.collision([58,3], [72,3], [72,50], [58,50])
.gravity('floor')
.gravityConst(.3)
;
player.axe = Crafty.e('2D')
.attr({ x: x*32, y: y*32, w:36, h:46 })
.addComponent('Collision');
if (collisionBox) player.addComponent('WiredHitBox');
if (collisionBox) player.axe.addComponent('WiredHitBox');
return player;
}
function createNative(x, y)
{
var nativeMan = Crafty.e('2D, DOM, nativeMan, Tweenable, Collision, Gravity, Ape, Enemy, NativeTypeAxe')
.attr({ x: x * 32, y: y * 32, z:1000 })
.origin('bottom center')
.collision([55,3], [75,3], [75,50], [55,50])
.gravity('floor')
.gravityConst(.1)
;
nativeMan.axe = Crafty.e('2D')
.attr({ x: x*32, y: y*32, w:30, h:46})
.addComponent('Collision');
if (collisionBox) nativeMan.addComponent('WiredHitBox');
if (collisionBox) nativeMan.axe.addComponent('WiredHitBox');
return nativeMan;
}
function createTree(x, y, treeType, treeNumber)
{
var randomDisplacement = Crafty.math.randomInt(-2,2);
var tree = Crafty.e('2D, DOM, tree'+treeType+', Tweenable, Collision, Tree')
.attr({ x: x, y: y + (randomDisplacement*5), z:800-treeNumber+randomDisplacement, treeType: treeType})
.origin('bottom center')
.collision([85,250], [160,150], [160,305+140], [85,305+140])
;
if (collisionBox) tree.addComponent('WiredHitBox');
return tree;
}
function createArrow()
{
Crafty.e('2D, DOM, nextStageArrow, Blink')
.attr({ x: stageWidth*(stage+1)-150, y: 50, z:1500 })
;
Crafty.audio.play("go");
}
function createBlood(x, y)
{
Crafty.e('2D, DOM, Tween, Blood, blood'+Crafty.math.randomInt(1,6))
.attr({ x: x, y: y, z:600, h:1 })
.tween({h: 30}, 65);
}
function increasePoints(x)
{
points += x;
pointsHUD.innerHTML = points;
}
function changeLife(newValue)
{
lifeHUD.innerHTML = newValue;
}
function createText(speaker, msg)
{
dialog = Crafty.e('2D, DOM, Text, TimedDialog')
.attr({ w: 220, h: 120, x: speaker.x-55, y: speaker.y-50, z:2001 })
.text(msg);
speaker.attach(dialog);
return dialog;
}