/
player_spaceship.ts
66 lines (53 loc) · 1.87 KB
/
player_spaceship.ts
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
/*
Project: Arena game
Author: Copyright (C) 2015, Atanas Laskov
License: BSD license, see LICENSE.md for more details.
http://www.atanaslaskov.com/arena/
*/
/// <reference path="spaceship.ts" />
// Spaceship controled by the player
//
class PlayerSpaceship extends Spaceship{
private playerScore: number;
private swing: number;
constructor(p: PolarCoordinate) {
super(p);
this.color = new Color(1.0, 1.0, 1.0);
this.hpMax = this.hp = 50.0;
this.playerScore = 0;
this.swing = 0.0;
document.addEventListener('keydown', (e) => {
if( e.keyCode == 37 ) this.direction = +1;
if( e.keyCode == 39 ) this.direction = -1;
if( e.keyCode == 32 ) this.prepareAttack();
});
document.addEventListener('keyup', (e) => {
if( e.keyCode == 37 ) this.direction = 0;
if( e.keyCode == 39 ) this.direction = 0;
});
document.getElementById("game_over").style.opacity = "0.0";
}
public animate(dt: number, origin_speed: number) {
super.animate(dt, origin_speed);
this.swing += 0.3*dt;
}
public render(renderer: Renderer, origin: Vector) {
super.render(renderer, origin);
renderer.rotation( this.position.angle - Math.PI*(Math.sin(this.swing)*0.04 + 0.5) );
}
// Ask the spaceship
public ask(sentence: DynamicMessage): DynamicMessage {
// Player spaceship overides the normal "destroy" message with "gameover"
if( sentence.verb == "gameover?" && this.hp <= 0 ) {
// The "game over" screen has CSS-transition on opacity, we don't need to animate it manually
document.getElementById("game_over").style.opacity = "1.0";
return { verb: "gameover!" };
}
// Somebody asked us if we are the player ship
if( (sentence.verb == "is?") && (sentence.argument == "player") ) {
return { verb: "is!" };
}
// Otherwise, ask the parent
return super.ask(sentence);
}
}