forked from sorenbs/bananabomber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameT.js
163 lines (145 loc) · 4.87 KB
/
gameT.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
window.onload = function() {
//start crafty
Crafty.init(50, 400, 320);
Crafty.canvas();
//turn the sprite map into usable components
Crafty.sprite(16, "sprite.png", {
grass1: [0,0],
grass2: [1,0],
grass3: [2,0],
grass4: [3,0],
flower: [0,1],
bush1: [0,2],
bush2: [1,2],
player: [0,3]
});
//method to randomy generate the map
function generateWorld() {
//generate the grass along the x-axis
for(var i = 0; i < 25; i++) {
//generate the grass along the y-axis
for(var j = 0; j < 20; j++) {
grassType = Crafty.randRange(1, 4);
Crafty.e("2D, canvas, grass"+grassType)
.attr({x: i * 16, y: j * 16});
//1/50 chance of drawing a flower and only within the bushes
if(i > 0 && i < 24 && j > 0 && j < 19 && Crafty.randRange(0, 50) > 49) {
Crafty.e("2D, DOM, flower, animate")
.attr({x: i * 16, y: j * 16})
.animate("wind", 0, 1, 3)
.bind("enterframe", function() {
if(!this.isPlaying())
this.animate("wind", 80);
});
}
}
}
//create the bushes along the x-axis which will form the boundaries
for(var i = 0; i < 25; i++) {
Crafty.e("2D, canvas, wall_top, bush"+Crafty.randRange(1,2))
.attr({x: i * 16, y: 0, z: 2});
Crafty.e("2D, DOM, wall_bottom, bush"+Crafty.randRange(1,2))
.attr({x: i * 16, y: 304, z: 2});
}
//create the bushes along the y-axis
//we need to start one more and one less to not overlap the previous bushes
for(var i = 1; i < 19; i++) {
Crafty.e("2D, DOM, wall_left, bush"+Crafty.randRange(1,2))
.attr({x: 0, y: i * 16, z: 2});
Crafty.e("2D, canvas, wall_right, bush"+Crafty.randRange(1,2))
.attr({x: 384, y: i * 16, z: 2});
}
}
//the loading screen that will display while our assets load
Crafty.scene("loading", function() {
//load takes an array of assets and a callback when complete
Crafty.load(["sprite.png"], function() {
Crafty.scene("main"); //when everything is loaded, run the main scene
});
//black background with some loading text
Crafty.background("#000");
Crafty.e("2D, DOM, text").attr({w: 100, h: 20, x: 150, y: 120})
.text("Loading")
.css({"text-align": "center"});
});
//automatically play the loading scene
Crafty.scene("loading");
Crafty.scene("main", function() {
generateWorld();
Crafty.c('CustomControls', {
__move: {left: false, right: false, up: false, down: false},
_speed: 3,
CustomControls: function(speed) {
if(speed) this._speed = speed;
var move = this.__move;
this.bind('enterframe', function() {
//move the player in a direction depending on the booleans
//only move the player in one direction at a time (up/down/left/right)
if(move.right) this.x += this._speed;
else if(move.left) this.x -= this._speed;
else if(move.up) this.y -= this._speed;
else if(move.down) this.y += this._speed;
}).bind('keydown', function(e) {
//default movement booleans to false
move.right = move.left = move.down = move.up = false;
//if keys are down, set the direction
if(e.keyCode === Crafty.keys.RA) move.right = true;
if(e.keyCode === Crafty.keys.LA) move.left = true;
if(e.keyCode === Crafty.keys.UA) move.up = true;
if(e.keyCode === Crafty.keys.DA) move.down = true;
this.preventTypeaheadFind(e);
}).bind('keyup', function(e) {
//if key is released, stop moving
if(e.keyCode === Crafty.keys.RA) move.right = false;
if(e.keyCode === Crafty.keys.LA) move.left = false;
if(e.keyCode === Crafty.keys.UA) move.up = false;
if(e.keyCode === Crafty.keys.DA) move.down = false;
this.preventTypeaheadFind(e);
});
return this;
}
});
//create our player entity with some premade components
player = Crafty.e("2D, canvas, player, controls, CustomControls, animate, collision")
.attr({x: 160, y: 144, z: 1})
.CustomControls(1)
.animate("walk_left", 6, 3, 8)
.animate("walk_right", 9, 3, 11)
.animate("walk_up", 3, 3, 5)
.animate("walk_down", 0, 3, 2)
.bind("enterframe", function(e) {
if(this.__move.left) {
if(!this.isPlaying("walk_left"))
this.stop().animate("walk_left", 10);
}
if(this.__move.right) {
if(!this.isPlaying("walk_right"))
this.stop().animate("walk_right", 10);
}
if(this.__move.up) {
if(!this.isPlaying("walk_up"))
this.stop().animate("walk_up", 10);
}
if(this.__move.down) {
if(!this.isPlaying("walk_down"))
this.stop().animate("walk_down", 10);
}
}).bind("keyup", function(e) {
this.stop();
})
.collision()
.onhit("wall_left", function() {
this.x += this._speed;
this.stop();
}).onhit("wall_right", function() {
this.x -= this._speed;
this.stop();
}).onhit("wall_bottom", function() {
this.y -= this._speed;
this.stop();
}).onhit("wall_top", function() {
this.y += this._speed;
this.stop();
});
});
};