-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.js
More file actions
138 lines (132 loc) · 3.44 KB
/
GameManager.js
File metadata and controls
138 lines (132 loc) · 3.44 KB
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
/**
* Manages the overall game state, input, rendering, updates, scenes.
*/
class GameManager
{
/**
Current game scene
*/
currentScene = null;
/**
Rendering context
*/
ctx = null;
/**
Keeps track of the previous update timestamp.
*/
prevTime = null;
/**
*/
debug = false;
/**
* Updates the game state and requests next update.
* @param {number} timestamp - The timestamp given by the frame, used for calculating elapsed time.
*/
update(timestamp)
{
// if this is the first run, use current timestamp, resulting in a 0 step
if(this.prevTime===null)
{
this.prevTime = timestamp;
}
// calculate elapsed time
const dT = timestamp-this.prevTime;
// render and update current scene if one exists and can be rendered
if(this.currentScene && this.ctx)
{
// the timestamps are in milliseconds, convert to floating point seconds
this.currentScene.update(dT/1000);
this.currentScene.draw(this.ctx);
}
// save the timestamp to calculate elapsed time next round
this.prevTime = timestamp;
// ensures rendering matches refresh rate
requestAnimationFrame((t)=>this.update(t));
}
/**
* Handles pointer (touch, mouse) movement
* @param {PointerEvent} e - the event from DOM
*/
handlePointerMove(e)
{
// this distinguishes second (and so on) touch on touch devices
if(e.isPrimary)
{
this.currentScene.handlePrimaryPointerMove(e);
}
else
{
this.currentScene.handleSecondaryPointerMove(e);
}
e.preventDefault();
}
/**
* Handles mouse button release / finger release
* @param {PointerEvent} e - the event from DOM
*/
handlePointerUp(e)
{
// this distinguishes second (and so on) touch on touch devices
if(e.isPrimary)
{
this.currentScene.handlePrimaryPointerUp(e);
}
else
{
this.currentScene.handleSecondaryPointerUp(e);
}
e.preventDefault();
}
/**
* Handles mouse button down and touch start
* @param {PointerEvent} e - the event from DOM
*/
handlePointerDown(e)
{
}
/**
* Handles mouse button click and touch "click"
* @param {PointerEvent} e - the event from DOM
*/
handlePointerClick(e)
{
// this distinguishes second (and so on) touch on touch devices
if(e.isPrimary)
{
this.currentScene.handlePrimaryPointerClick(e);
}
else
{
this.currentScene.handleSecondaryPointerClick(e);
}
e.preventDefault();
}
/**
* Handles key down from the keyboard
* @param {PointerEvent} e - the event from DOM
*/
handleKeyDown(e)
{
console.log("gamemanager keydown");
this.currentScene.handleKeyDown(e);
e.preventDefault();
}
/**
* Handles key up from the keyboard
* @param {PointerEvent} e - the event from DOM
*/
handleKeyUp(e)
{
this.currentScene.handleKeyUp(e);
e.preventDefault();
}
/**
* Handles key press from the keyboard
* @param {PointerEvent} e - the event from DOM
*/
handleKeyPress(e)
{
this.currentScene.handleKeyPress(e);
e.preventDefault();
}
}