Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/algaes-escapade/js/lib/player.js
1037 views
1
/**
2
* Represents a player. Players have control over multiple playables, but only
3
* one playable at a time.
4
*
5
* @author David North
6
*/
7
include_once(['lib/playable.js']);
8
function player()
9
{
10
//The maximum X velocity a player can trvel (heading right)
11
const MAX_X_VELOCITY = 200;
12
13
//The minimum X velocity a player can trvel (heading left)
14
const MIN_X_VELOCITY = -200;
15
16
//The minimum Y velocity a player can trvel (heading up)
17
const MIN_Y_VELOCITY = -350;
18
19
/**
20
* @var array An array of playables at the players disposal
21
*/
22
var _playables = new gamejs.sprite.Group();
23
24
/**
25
* @var float How fast the player is currently walking (minus
26
* figure represents left, positive right)
27
*/
28
var _walkVelocity = 0;
29
30
/**
31
* @var int The number of clones created
32
*/
33
var _numClones = 0;
34
35
//Start the player with a single playable
36
_playables.add( new playable() );
37
38
/**
39
* @var int The current index of the playable currently under the
40
* players control
41
*/
42
var _currentIndex = 0;
43
44
var _selectedImg = gamejs.image.load('img/selected.png');
45
46
/**
47
* Handles player input.
48
*
49
* @param world world The world this event came from
50
* @param gamejs.Event event The event that fired
51
*/
52
this.handleInput = function(event){
53
//If a key has been pressed then check it to see if an
54
//action needs taking place
55
if ( event.type === gamejs.event.KEY_DOWN )
56
{
57
switch( event.key )
58
{
59
//The space, w, or up key denotes a jump. The player is not allowed
60
//to jump if they are already falling or jumping
61
case gamejs.event.K_SPACE:
62
case gamejs.event.K_w:
63
case gamejs.event.K_UP:
64
if ( this.getCurrentPlayable().getMovement() == 'walk' )
65
{
66
this.setVelocity( this.getVelocity().x, MIN_Y_VELOCITY );
67
this.getCurrentPlayable().setMovement('jump');
68
}
69
break;
70
71
//The A key, or left arrow starts to move the player left
72
case gamejs.event.K_a:
73
case gamejs.event.K_LEFT:
74
_walkVelocity = MIN_X_VELOCITY;
75
break;
76
77
//The D key, or right arrow starts to move the player right
78
case gamejs.event.K_d:
79
case gamejs.event.K_RIGHT:
80
_walkVelocity = MAX_X_VELOCITY;
81
break;
82
83
//The C key clones a playable, so that the player can use
84
//that instead
85
case gamejs.event.K_c:
86
if ( _numClones < 100 )
87
{
88
this.clone();
89
_numClones++;
90
}
91
break;
92
93
//The Tab key switches between the playables that the
94
//player can control
95
case gamejs.event.K_TAB:
96
_walkVelocity = 0;
97
this.moveToNext();
98
}
99
}
100
else if ( event.type === gamejs.event.KEY_UP )
101
{
102
switch( event.key )
103
{
104
//Letting go of direction events will stop the player dead
105
case gamejs.event.K_a:
106
case gamejs.event.K_LEFT:
107
case gamejs.event.K_d:
108
case gamejs.event.K_RIGHT:
109
_walkVelocity = 0;
110
break;
111
}
112
}
113
}
114
115
/**
116
* Returns the number of clonmes that the player has created
117
*
118
* @return int
119
*/
120
this.getNumClones = function(){
121
return _numClones;
122
}
123
124
/**
125
* Sets the velocity of the current playable
126
*
127
* @param float x The X velocity
128
* @param float y The Y velocity
129
*
130
* @return player
131
*/
132
this.setVelocity = function( x, y ){
133
var playable = this.getCurrentPlayable();
134
playable.setVelocity( x, y );
135
136
return this;
137
}
138
139
/**
140
* Returns an object taht contains the X and Y velocity of the current
141
* playable
142
*
143
* @return object
144
*/
145
this.getVelocity = function(){
146
return this.getCurrentPlayable().getVelocity();
147
}
148
149
/**
150
* Updates all of the playable objects
151
*
152
* @param int msDuration
153
*
154
* @return player
155
*/
156
this.update = function( msDuration ){
157
if ( _walkVelocity )
158
{
159
this.setVelocity( _walkVelocity, this.getVelocity().y );
160
}
161
162
_playables.update( msDuration );
163
return this;
164
}
165
166
/**
167
* Draws all of the playable objects to the surface
168
*
169
* @param object mainSurface
170
*
171
* @return player
172
*/
173
this.draw = function( mainSurface ){
174
_playables.draw( mainSurface );
175
176
var x = this.getCurrentPlayable().getX();
177
var y = this.getCurrentPlayable().getY();
178
179
x += (this.getCurrentPlayable().rect.width / 2);
180
x -= (_selectedImg.getSize()[0] / 2);
181
182
y -= (_selectedImg.getSize()[1] + 2);
183
184
var rect = new gamejs.Rect([x, y])
185
mainSurface.blit(_selectedImg, rect);
186
return this;
187
}
188
189
/**
190
* Moves to the next available playable for control. If the last playable
191
* is selected then the first element is sed instead
192
*
193
* @return player
194
*/
195
this.moveToNext = function(){
196
if ( (_currentIndex + 1) == this.getPlayables().sprites().length )
197
{
198
_currentIndex = 0;
199
}
200
else
201
{
202
_currentIndex++
203
}
204
205
return this;
206
}
207
208
/**
209
* Gets all of the playables available to the player
210
*
211
* @return array
212
*/
213
this.getPlayables = function(){
214
return _playables;
215
}
216
217
/**
218
* Gets the current playable object that the player is in control of
219
*
220
* @return playable
221
*/
222
this.getCurrentPlayable = function(){
223
return this.getPlayables().sprites()[_currentIndex];
224
}
225
226
/**
227
* Clones the current playable and adds it to the playables at the players
228
* disposal
229
*
230
* @return player
231
*/
232
this.clone = function(){
233
//Get the template for the clone, and create a clone to the left
234
//of the current playable
235
var template = this.getCurrentPlayable();
236
var clone = new playable();
237
var newX = ( template.getX() - (template.rect.width * 2) );
238
239
clone.setPosition(newX, template.getY() - template.image.getSize()[1]);
240
241
_playables.add( clone );
242
243
return this;
244
}
245
}
246
247