-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_game.js
203 lines (175 loc) · 7.18 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
import { AssetManager } from "./assetManager.js"
import { Camera } from "./camera.js"
import { LoadingState, MenuState, LevelSelectState, FadeIn, PlayState, PauseState, RestartConfirmState, GSM, FadeOut, LevelCompleteState, GameOverState, OptionsState } from "./state.js"
import { Input } from "./input.js"
import { Map, BackgroundLayer } from "./map.js"
import { Hero, HeroStateMachine } from "./hero.js"
import { UI } from "./ui.js"
import { CollisionBlock } from "./collisionblock.js"
import { drawCharacterStatus, drawEnemyhealthbar, drawPlayerenergybar, drawPlayerhealthbar, drawPlayerScore, drawHeroStatus } from "./utils.js"
import { Forest, Ruins } from "./levels.js"
import { Enemy } from "./enemypool.js"
import { AudioControl } from "./audio.js"
export class Game{
constructor(canvas){
this.canvas = canvas;
this.width = this.canvas.width;
this.height = this.canvas.height;
this.tileSize = 48;
this.rows = Math.ceil(this.height/this.tileSize);
this.columns = Math.ceil(this.width/this.tileSize);
//UI, INPUT AND ASSETMANAGER INSTANCES
this.ui = new UI();
this.ui.fullscreenBtn.addEventListener('click', ()=>{
this.toggleFullscreen();
});
this.input = new Input(this);
this.assetManager = new AssetManager(this);
//MAP, PLAYER AND CAMERA
this.map = new Map(this);
this.background = new BackgroundLayer(this, 0.3);
this.clouds = new BackgroundLayer(this, 0.3);
this.bushes = new BackgroundLayer(this, 0.7);
this.backgroundLayers = [this.background, this.clouds, this.bushes];
this.player = new Hero(this);
this.enemy = new Enemy(this);
this.enemy.start(400, 0);
this.camera = new Camera(this);
this.camera.follow(this.player);
//game rows and columns
this.rows = undefined;
this.columns = undefined;
//collisionBlock instance
this.collisionBlock = new CollisionBlock(this);
//collisonblocks data
this.solidBlocks = undefined;
this.immobile_liquidBlocks = undefined;
this.mobileBlocks = undefined;
this.exitDoor = undefined;
//PAUSE AND PLAY
this.paused = false;
//DEBUG MODE
this.debug = false;
//BATTLE ZONE DIMENSION
//this is temporary; later, each enemy type will have different battle zone ranges
this.battleZone = 250; //a square battle zone, 300 width x 300 height
//ENEMY POOLS
this.gameTotalBasicEnemies = 20;
this.numberOfBasicEnemies = undefined;
this.numberOfBosses = undefined;
this.enemies = [];
this.bosses = [];
//LEVELS
//create all enemies for the entire game duration created here:
this.createEnemyPool();
this.levels = [new Forest(this), new Ruins(this)];
//the currentLevel sets all the undefined member variables pertaining to each level
this.currentLevel = this.levels[0];
//STATE MACHINES
//game states
this.gsm = new GSM(this);
this.STATES = {
LOADING: new LoadingState(this),
MENU: new MenuState(this),
LEVELSELECT: new LevelSelectState(this),
FADEIN: new FadeIn(this),
PLAY: new PlayState(this),
PAUSE: new PauseState(this),
FADEOUT: new FadeOut(this),
LEVELCOMPLETE: new LevelCompleteState(this),
RESTARTCONFIRM: new RestartConfirmState(this),
OPTIONS: new OptionsState(this)
};
//hero and enemy state machines
this.heroStateMachine = new HeroStateMachine(this);
//AUDIO INPUT
this.audio = new AudioControl(this);
//TOGGLE FULLSCREEN AND DEBUG MODES
this.ui.fullscreenBtn.addEventListener('click', ()=>{
this.toggleFullscreen();
});
this.ui.play_specialBtn.addEventListener('mouseup', () => {
this.debug = !this.debug;
});
//FLOATING MESSAGES
this.floatingMessages = [];
//console
}
render(ctx, deltaTime){
ctx.clearRect(0,0, this.width, this.height);
this.gsm.renderState(ctx, deltaTime);
//temp
this.enemies.forEach((enemy, i)=>{
if(!enemy.free){
drawEnemyhealthbar(this, ctx, enemy);
ctx.fillStyle = "gold";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "30px Arial";
ctx.fillText(i, -this.camera.viewportX+enemy.collisionbox.x + enemy.collisionbox.width*0.5, -this.camera.viewportY+enemy.collisionbox.y);
}
})
drawCharacterStatus(ctx, this.enemies[0]);
drawPlayerhealthbar(ctx, this.player);
drawPlayerenergybar(ctx, this.player);
drawPlayerScore(ctx, this.player);
drawHeroStatus(ctx, this.player);
//this.drawGrid(ctx);
this.floatingMessages.forEach(message=>{message.render(ctx)})
}
createEnemyPool(){
for(let i = 0; i < this.gameTotalBasicEnemies; ++i){
this.enemies.push(new Enemy(this));
}
}
getOneFreeEnemy(){
for(let i = 0; i < this.enemies.length; ++i){
const enemy = this.enemies[i];
if(enemy.free) return enemy;
}
}
update(ctx, deltaTime){
//update game rows and columns
this.rows = this.map.height/this.tileSize;
this.columns = this.map.width/this.tileSize;
this.floatingMessages.forEach(message=>{message.update()})
this.floatingMessages = this.floatingMessages.filter(message=> !message.markedForDeletion );
this.gsm.updateState(ctx, deltaTime);
}
rectangularCollision(a, b){
return (
a.x + a.width >= b.x &&
a.x <= b.x + b.width &&
a.y + a.height >= b.y &&
a.y <= b.y + b.height
);
}
randomFloat(min, max){
return Math.random() * (max - min) + min;
}
randomInt(min, max){
return Math.floor (Math.random() * (max-min) + min);
}
toggleFullscreen(){
if(!document.fullscreenElement){
document.documentElement.requestFullscreen();
}else if(document.exitFullscreen){
document.exitFullscreen();
}
}
drawGrid(ctx){
for(let row = 0; row < this.rows; ++ row){
for(let col = 0; col < this.columns; ++col){
ctx.strokeStyle = "black";
ctx.strokeRect(-this.camera.viewportX+col*this.tileSize, -this.camera.viewportY+row*this.tileSize, this.tileSize, this.tileSize);
//show numbers on grid
ctx.fillStyle = "white";
ctx.font = "13px Arial";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
const tileCentre = this.tileSize/2;
//ctx.fillText(-this.camera.viewportX+col+","+row, -this.camera.viewportY+col*this.tileSize + tileCentre, row*this.tileSize + tileCentre);
}
}
}
}