3
3
import Globals from "./globals.js" ;
4
4
import * as Utils from "./utilities.js" ;
5
5
6
- export function Tick ( runtime )
6
+ // TypeScript note: import the MonsterInstance class so it can be used as a type
7
+ import MonsterInstance from "./monster.js" ;
8
+
9
+ export function Tick ( runtime : IRuntime )
7
10
{
8
11
// The tick event runs every frame. The game needs to be advanced
9
12
// by the amount of time in delta-time, also known as dt.
@@ -14,7 +17,8 @@ export function Tick(runtime)
14
17
15
18
// Next handle all monster's movement. Note this calls a method in
16
19
// the custom MonsterInstance class defined in Monster.js.
17
- for ( const monsterInstance of runtime . objects . Monster . instances ( ) )
20
+ // TypeScript note: iterate Monster instances as the custom MonsterInstance class.
21
+ for ( const monsterInstance of runtime . objects . Monster . instances < MonsterInstance > ( ) )
18
22
{
19
23
monsterInstance . Move ( ) ;
20
24
}
@@ -39,19 +43,19 @@ export function Tick(runtime)
39
43
}
40
44
41
45
// Finally, always display score in the status text object.
42
- const statusTextInstance = runtime . objects . Status . getFirstInstance ( ) ;
46
+ const statusTextInstance = runtime . objects . Status . getFirstInstance ( ) ! ;
43
47
statusTextInstance . text = "Score: " + Globals . score ;
44
48
}
45
49
46
- function MovePlayer ( runtime )
50
+ function MovePlayer ( runtime : IRuntime )
47
51
{
48
52
// Use a playerInst local variable as a shorthand way to refer to
49
53
// the playerInstance global variable in this function, since
50
54
// it is used many times. Similarly create local variables to
51
55
// reference runtime.keyboard and runtime.dt.
52
56
const playerInst = Globals . playerInstance ;
53
57
const dt = runtime . dt ;
54
- const keyboard = runtime . keyboard ;
58
+ const keyboard = runtime . keyboard ! ;
55
59
56
60
// The player is destroyed if a monster catches them. Don't try to
57
61
// handle the player movement if the only instance was destroyed.
@@ -88,12 +92,12 @@ function MovePlayer(runtime)
88
92
runtime . layout . scrollTo ( playerInst . x , playerInst . y ) ;
89
93
90
94
// Always make the player look in the direction of the mouse cursor
91
- const mouse = runtime . mouse ;
95
+ const mouse = runtime . mouse ! ;
92
96
playerInst . angle = Utils . angleTo ( playerInst . x , playerInst . y ,
93
97
mouse . getMouseX ( ) , mouse . getMouseY ( ) ) ;
94
98
}
95
99
96
- function MoveBullet ( inst , dt )
100
+ function MoveBullet ( inst : InstanceType . Bullet , dt : number )
97
101
{
98
102
// Move bullets forward at their angle at a speed of 600 pixels per second.
99
103
// This is similar to the Bullet behavior's movement.
@@ -102,7 +106,7 @@ function MoveBullet(inst, dt)
102
106
inst . y += Math . sin ( inst . angle ) * speed * dt ;
103
107
}
104
108
105
- function CheckBulletHitMonster ( bulletInstance , runtime )
109
+ function CheckBulletHitMonster ( bulletInstance : InstanceType . Bullet , runtime : IRuntime )
106
110
{
107
111
// Save a reference to the Explosion object type to help
108
112
// keep the code short and readable.
@@ -111,7 +115,7 @@ function CheckBulletHitMonster(bulletInstance, runtime)
111
115
// Check if a bullet has collided with any monster. To do this it
112
116
// must check against every Monster instance. This is similar to
113
117
// what the 'Is overlapping' condition does.
114
- for ( const monsterInstance of runtime . objects . Monster . instances ( ) )
118
+ for ( const monsterInstance of runtime . objects . Monster . instances < MonsterInstance > ( ) )
115
119
{
116
120
// Test if the bullet instance overlaps this monster instance,
117
121
// indicating a collision.
@@ -140,7 +144,7 @@ function CheckBulletHitMonster(bulletInstance, runtime)
140
144
}
141
145
}
142
146
143
- function FadeExplosion ( inst , dt )
147
+ function FadeExplosion ( inst : InstanceType . Explosion , dt : number )
144
148
{
145
149
// Fade out explosions over 0.5 seconds, and destroy it once it
146
150
// becomes invisible. This is similar to the Fade behavior.
@@ -150,7 +154,7 @@ function FadeExplosion(inst, dt)
150
154
inst . destroy ( ) ;
151
155
}
152
156
153
- export function OnMouseDown ( e , runtime )
157
+ export function OnMouseDown ( e : MouseEvent , runtime : IRuntime )
154
158
{
155
159
// The left mouse button is number 0. Ignore any other mouse buttons.
156
160
if ( e . button !== 0 )
@@ -172,7 +176,7 @@ export function OnMouseDown(e, runtime)
172
176
bulletInstance . angle = playerInst . angle ;
173
177
}
174
178
175
- export function OnKeyDown ( e , runtime )
179
+ export function OnKeyDown ( e : KeyboardEvent , runtime : IRuntime )
176
180
{
177
181
// Pressing space when the player is destroyed restarts the game.
178
182
if ( ! Globals . playerInstance && e . key === " " )
0 commit comments