by Dmitry & Victoria
characters walk & evasion
run(){
let x = this.player.x - this.x;
let y = this.player.y - this.y;
if(Math.sqrt(x*x+y*y) < 2){
this.speed = 3;
this.direction = -this.player.direction;
} else this.speed = .7;
}
walk(distance, direction) {
const dx = Math.cos(direction) * distance;
const dy = Math.sin(direction) * distance;
const in_the_x_way = this.map.get(this.x + dx, this.y);
const in_the_y_way = this.map.get(this.x, this.y + dy);
if ((in_the_x_way == 2 || in_the_y_way == 2) ||
(in_the_x_way == 1 || in_the_y_way == 1)){
this.direction = direction + this.CIRCLE/6;
};
if (in_the_x_way <= 0) this.x += dx;
if (in_the_y_way <= 0) this.y += dy;
this.move('img/npc/npc');
};
wonderAround() {
this.count += 1;
this.run();
this.walk(0.05 * this.speed, this.direction);
};
sound collision & gameloop termination
...
this.sounds = new Sounds();
this.noises = new Sounds();
this.loop = new GameLoop(this, this.endGame);
this.camera = new Camera(document.getElementById('display'), 640, 0.8, this.state, this.CIRCLE);
};
loadGame() {
this.obj_sounds = new Sounds(this.map, this.loop, this.state);
this.map = new Map(32, this.state);
...
startGame() {
document.querySelector('.text').style.display = 'none';
document.querySelector('canvas').style.display = 'block';
this.loop.start((seconds) => {
if (this.state.lightning) this.map.lightning(seconds);
this.map.update();
this.changeAmbient();
this.player.update(this.controls.states, this.map, seconds);
this.camera.render(this.player, this.map);
});
};
start(callback) {
this.callback = callback;
requestAnimationFrame(this.frame);
return;
};
frame(time) {
const seconds = (time - this.lastTime) / 1000;
this.lastTime = time;
if (seconds < 0.2) this.callback(seconds);
if (this.game_ending) {
this.endGame()
return;
}
requestAnimationFrame(this.frame);
};