-
Notifications
You must be signed in to change notification settings - Fork 5
/
game.js
220 lines (154 loc) · 4.95 KB
/
game.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
let player;
let gravity = 0.3;
let score = 0;
let layer = []; //this creates a list (array) that will store all the layers that were in the tiledmap
let layerImages = []; //this list holds all the image (drawing) information for the layers in the map.
var tiledmap;
var pickupImage;
//create the values of the different world layers
let BACKGROUND = 0;
let GROUND = 1;
let LADDERS = 2;
let DEATH = 3;
let FOREGROUND = 4;
let pickups = []; //this is a list of the pickups
//sound variables
let kaching;
let music;
let soundOn = false;
//Preload runs once before Setup
function preload(){
tiledmap = loadTiledMap("world", "images");
pickupImage = loadImage("images/coin.png");
kaching = loadSound("audio/coin.wav");
music = loadSound("audio/gameloop.mp3");
}
//Setup function for the entire game. It runs once.
function setup(){
createCanvas(720,360);
//create the player
player = createSprite(100,100);
player.velocity.x=0;
player.setDefaultCollider();
player.alive = true;
player.addAnimation('stand', 'anims/tile011.png');
player.addAnimation('walk', 'anims/tile011.png', 'anims/tile008.png', 'anims/tile005.png');
//these lines of code load up the layer and images data from the world map
layer = getTilemapLayers(tiledmap);
layerImages = getTilemapImages(tiledmap);
//create the pickup
for (let i=0; i<10; i = i +1){
let pickup = new Pickup(100 + i * 30 , 350);
pickups.push(pickup);
}
}
//This function runs for every frame of the game. Around 60 frames per second.
function draw(){
background(72,72,150);
checkInput();
checkWorldBounds(player, tiledmap);
//this code draws the layers on the screen
image(layerImages[BACKGROUND], 0, 0);
image(layerImages[GROUND], 0, 0);
image(layerImages[LADDERS], 0, 0);
image(layerImages[DEATH], 0, 0);
drawSprite(player);
handlePickups();
image(layerImages[FOREGROUND], 0, 0);
//draw the scoreboard
noStroke();
fill('yellow');
rect(screenLeft(), screenTop(), width, textAscent()+textDescent());
fill('black');
textSize(16);
text("Score: "+score, screenLeft(), screenTop() + textAscent());
focusCamera(player, tiledmap);
//handling background music
if (soundOn === true){
if (music.isPlaying() === false){
music.loop();
music.setVolume(0.2);
}
}
if (soundOn === false){
if(keyIsDown){
soundOn = true;
}
}
}
function handlePickups(){
for (let i=pickups.length-1; i>=0; i = i-1){
if (pickups[i].checkHit(player)){
pickups.splice(i,1);
} else {
pickups[i].show();
}
}
}
function die(){
player.velocity.x = 0;
player.velocity.y = -10;
player.rotationSpeed = 20;
}
//this code checks for the user input and moves the player accordingly
function checkInput(){
//checks if alive or dead
let isOnDeath = isInContact(player, layer[DEATH]);
if (isOnDeath.any){
player.alive = false;
die();
}
if (player.alive === false){
return;
}
//allowing movement
let touchingGround = isInContact(player, layer[GROUND]);
player.velocity.y = player.velocity.y + gravity;
//moving left and right
if (keyIsDown(LEFT_ARROW)){
player.changeAnimation('walk');
player.mirrorX(-1);
player.velocity.x = player.velocity.x - 1
} else if (keyIsDown(RIGHT_ARROW)){
player.changeAnimation('walk');
player.mirrorX(1);
player.velocity.x = player.velocity.x + 1;
}
if (player.velocity.x < -5) {
player.velocity.x = -5;
}
if (player.velocity.x > 5) {
player.velocity.x = 5;
}
if (player.velocity.x > -1 && player.velocity.x <1){
player.changeAnimation('stand');
player.velocity.x = 0;
}
player.velocity.x = 0.9 * player.velocity.x;
///deal with vertical movemenct
playerBrake(player, touchingGround);
//jumping
if ( keyIsDown(32) && touchingGround.below ) {
player.velocity.y = -5;
}
//climbing
let onLadder = isInContact(player, layer[LADDERS]);
if (onLadder.any){
if (keyIsDown(UP_ARROW)){
player.velocity.y=0;
player.position.y = player.position.y -5;
} else if (keyIsDown(DOWN_ARROW)) {
player.velocity.y =0;
if (touchingGround.below){
player.position.y = player.position.y + touchingGround.belowDistance;
} else {
player.position.y = player.position.y + 5;
}
} else {
player.velocity.y = 0;
}
if (keyIsDown(32)){
player.velocity.y = -5;
}
}
}