-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceship.ts
154 lines (121 loc) · 4.2 KB
/
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
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
/*
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="vector.ts" />
/// <reference path="polar_coordinate.ts" />
/// <reference path="dynamic_object.ts" />
/// <reference path="color.ts" />
/// <reference path="renderer.ts" />
/// <reference path="projectile.ts" />
// Base class for a spaceship
//
class Spaceship implements DynamicObject{
public speed: number;
public direction: number;
public position: PolarCoordinateAreal;
public hp: number;
public hpMax: number;
public color: Color;
protected projectile: Projectile;
protected isJumping: boolean;
protected jumpAngle: number;
protected jumpRadius: number;
protected jumpDone: ()=>void;
constructor(p: PolarCoordinate) {
this.position = new PolarCoordinateAreal(p.angle, p.radius - 0.05, 0.05);
this.speed = 1.0;
this.direction = +0;
this.projectile = null;
this.hpMax = 10.0;
this.hp = 10.0;
this.color = new Color(0.0, 1.0, 0.0);
this.isJumping = false;
this.jumpAngle = 0.0;
this.jumpRadius = 1.0;
}
// Animate the spaceship
//
public animate(dt: number, origin_speed: number) {
// If the ship is in the process of jumping, animate the radius and angle
if( this.isJumping ) {
this.position.radius += this.speed * dt;
this.jumpAngle += 2.0 * (this.position.angle - this.jumpAngle) * dt;
if( this.position.radius >= this.jumpRadius ) {
this.position.radius = this.jumpRadius;
this.isJumping = false;
if( this.jumpDone ) this.jumpDone();
}
}
else {
// Otherwise, animate only the angle
this.position.angle += dt * (this.speed + origin_speed) * this.direction;
}
}
// Display the spaceship
//
public render(renderer: Renderer, origin: Vector) {
var position: Vector = Vector.plus( this.position.vector(), origin );
var angle: number = (this.isJumping? this.jumpAngle : this.position.angle) - Math.PI*0.5;
renderer.style( this.color, 1 );
renderer.spaceship( position, angle, this.position.areal );
renderer.style( new Color(this.color.r, this.color.g, this.color.b, 0.5), 10.0 );
renderer.marker( position, 2.0, 1.0);
renderer.style( this.color, 3.0 );
renderer.marker( position, 10.0, this.hp / this.hpMax);
}
// Communicate with the spaceship
//
public ask(sentence: DynamicMessage): DynamicMessage {
// Somebody asked the Spaceship if it's time to get discarded
if( sentence.verb == "discard?" && this.hp <= 0 ) {
// Yes, time to live has expired.
return { verb: "discard!" };
}
// Somebody asked the Spaceship to follow to new position
if( sentence.verb == "follow!" ) {
var jumpRadius = <number> sentence.argument - 0.05;
if( this.isJumping ) this.jumpRadius = jumpRadius; // Set new follow target
else this.position.radius = jumpRadius; // Follow immediately
return { verb: "follow!" };
}
// Somebody asked the Spaceship if it wants to attack
if( sentence.verb == "attack?" && this.projectile ) {
// Yes, we want to attack. Reply with the projectile
var msg = { verb: "attack!", argument: this.projectile };
this.projectile = null;
return msg;
}
// Somebody the Spaceship to take damage
if( sentence.verb == "damage!" ) {
this.hp = Math.max(0.0, this.hp-sentence.argument);
return { verb: "damage!" };
}
// Somebody asked us if we are a Spaceship
if( (sentence.verb == "is?") && (sentence.argument == "spaceship") ) {
return { verb: "is!" };
}
// Otherwise just smile
return { verb: "smile!" };
}
// Prepare for attack
//
public prepareAttack(){
var p = this.position.copy();
this.projectile = new Projectile(
new PolarCoordinate(p.angle, p.radius),
this.color
);
}
// Prepare for evasive jump
//
public prepareJump( done: ()=>void = null ){
this.isJumping = true;
this.jumpAngle = this.position.angle;
this.jumpDone = done;
this.position.angle = this.position.angle - Math.PI;
this.position.radius = (-1.0)*this.position.radius;
}
}