Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/algaes-escapade/js/lib/playable.js
1038 views
1
/**
2
* Represents a playable sprite. This is manipulated by the player object which
3
* determines which playable should be modified
4
*
5
* @author David North
6
*/
7
function playable()
8
{
9
//Load the variables required by gamejs.sprite.Sprite
10
playable.superConstructor.apply(this, [0, 0]);
11
this.image = gamejs.image.load('img/player.png');
12
13
this.rect = new gamejs.Rect([0, 0]);
14
15
/**
16
* @var int The amount of health the playable has
17
*/
18
var _health = 100;
19
20
/**
21
* @var float The velocity left to right that the playable is experiencing
22
*/
23
var _velocityX = 0.0;
24
25
/**
26
* @var float The velocity bottom to top that this playable is experiencing
27
*/
28
var _velocityY = 0.0;
29
30
/**
31
* @var string How the player is currently moving (walk, jump, fall etc.)
32
*/
33
var _moveType = '';
34
35
/**
36
* Sets how the player is currently moving, and generates the correct
37
* sprite image
38
*
39
* @param string type The type of movement
40
*
41
* @return playable
42
*/
43
this.setMovement = function( type ){
44
//Only change the type if it has actually changed. No need to do any
45
//processing otherwise
46
if ( type != _moveType )
47
{
48
//Get the old size so that we can modify the X and Y co-ordinates
49
//accordingly if the sprite changes height or width
50
var oldSize = this.image.getSize();
51
52
//Set the correct sprite image depending on the type of movement
53
switch(type)
54
{
55
case 'walk':
56
this.image.crop( new gamejs.Rect([0,0], [46,55] ));
57
break;
58
case 'jump':
59
this.image.crop( new gamejs.Rect([0,55], [46,69] ));
60
break;
61
case 'fall':
62
this.image.crop( new gamejs.Rect([46,55], [46,69] ));
63
break;
64
}
65
66
//Get and set the new sizes
67
var _size = this.image.getSize();
68
this.rect.width = _size[0];
69
this.rect.height = _size[1];
70
71
//Set the new X and Y co-ordinates
72
this.rect.x += oldSize[0] - _size[0];
73
this.rect.y += oldSize[1] - _size[1];
74
75
_moveType = type;
76
}
77
78
return this;
79
}
80
81
/**
82
* Returns the current movement type of the playable
83
*
84
* @return string
85
*/
86
this.getMovement = function(){
87
return _moveType;
88
}
89
90
/**
91
* Sets the position of the object
92
*
93
* @param float x The X co-ordinate
94
* @param float y The Y co-ordinate
95
*
96
* @return playable
97
*/
98
this.setPosition = function(x, y){
99
this.setX(x);
100
this.setY(y);
101
102
return this;
103
}
104
105
/**
106
* Sets the X value of the playable (useful when initiating a new playable,
107
* teleporting, etc)
108
*
109
* @param float x
110
*
111
* @return playable
112
*/
113
this.setX = function( x ){
114
if ( typeof(x) != 'number')
115
{
116
throw 'Value for X must be a number';
117
}
118
119
this.rect.x = x;
120
return this;
121
}
122
123
/**
124
* Gets the current X position
125
*
126
* @return float
127
*/
128
this.getX = function(){
129
return this.rect.x;
130
}
131
132
/**
133
* Sets the Y value of the playable (useful when initiating a new playable,
134
* teleporting, etc)
135
*
136
* @param float y
137
*
138
* @return playable
139
*/
140
this.setY = function( y ){
141
if ( typeof(y) != 'number')
142
{
143
throw 'Value for Y must be a number';
144
}
145
146
this.rect.y = y;
147
return this;
148
}
149
150
/**
151
* Gets the current Y position
152
*
153
* @return float
154
*/
155
this.getY = function(){
156
return this.rect.y;
157
}
158
159
/**
160
* Sets the velocity of the playable so that the speed in one way or
161
* another can be set
162
*
163
* @param float x The velocity left to right
164
* @param float y The velocity bottom to top
165
*
166
* @return playable
167
*/
168
this.setVelocity = function( x, y ){
169
if ( typeof(x) != 'number')
170
{
171
throw 'Value for X must be a number';
172
}
173
174
if ( typeof(y) != 'number')
175
{
176
throw 'Value for Y must be a number';
177
}
178
179
_velocityX = x;
180
_velocityY = y;
181
return this;
182
}
183
184
/**
185
* Gets the X and Y velocity for this playable object by returning an object
186
* with an x and y parameter
187
*
188
* @return object
189
*/
190
this.getVelocity = function(){
191
return { 'x':_velocityX, 'y':_velocityY };
192
}
193
194
/**
195
* Updates the object, ready for the next draw request
196
*
197
* @param msDuration
198
*
199
* @return playable
200
*/
201
this.update = function( msDuration ){
202
//Set the default movement to wealking, if it hasn't already been set
203
if ( this.getMovement() == '' )
204
{
205
this.setMovement('walk');
206
}
207
208
//If the player is falling then set that sprite up. Other movement
209
//types are dealt with outside this object
210
if ( this.getVelocity().y > 0 )
211
{
212
this.setMovement('fall');
213
}
214
215
//Calculate the distance the playable has moved since the last frame
216
var distanceX = (this.getVelocity().x * (msDuration/1000));
217
var distanceY = (this.getVelocity().y * (msDuration/1000));
218
219
//Move the playable the calculated distance
220
this.rect.moveIp( distanceX, distanceY );
221
222
return this;
223
}
224
}
225
226
//Extend the playable object so that the parent is the sprite
227
gamejs.utils.objects.extend(playable, gamejs.sprite.Sprite);
228
229