Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/algaes-escapade/js/lib/goal.js
1038 views
1
/**
2
* The end goal of the game. All of the goals present in the game need to be
3
* active before the game is complete
4
*
5
* @author David North
6
*/
7
function goal()
8
{
9
//Load the variables required by gamejs.sprite.Sprite
10
goal.superConstructor.apply(this, [0, 0]);
11
this.image = gamejs.image.load('img/goal.png');
12
13
var _size = this.image.getSize();
14
this.rect = new gamejs.Rect([0, 0], [_size[0], _size[1]]);
15
16
/**
17
* @var boolean Whether the goal has been activated
18
*/
19
var _active = false;
20
21
/**
22
* Sets the position of the object
23
*
24
* @param float x The X co-ordinate
25
* @param float y The Y co-ordinate
26
*
27
* @return goal
28
*/
29
this.setPosition = function(x, y){
30
this.rect.x = x;
31
this.rect.y = y;
32
33
return this;
34
}
35
36
/**
37
* Returns whether or not the goal has been activated
38
*
39
* @return boolean
40
*/
41
this.isActive = function(){
42
return _active;
43
}
44
45
/**
46
* Updates the object, ready for the next draw request
47
*
48
* @param msDuration
49
*
50
* @return goal
51
*/
52
this.update = function(msDuration){
53
//The default state for thisobject is deactivated, unless a
54
//player has collided with it
55
_active = false;
56
57
return this;
58
}
59
60
/**
61
* Handles the collision between a playable and this object
62
*
63
* @return goal
64
*/
65
this.handleCollision = function( playable ){
66
//Modify the rectangle. The player shouldn't end the level until
67
//they are fully within the tube
68
var targetX = this.rect.x + (this.rect.width / 2);
69
targetX += (playable.rect.width / 2);
70
71
var targetY = this.rect.y;
72
var rect = new gamejs.Rect([targetX, targetY], [40, 144]);
73
74
//Check if the player has collided with this goal, and whether it
75
//should be activated
76
_active = playable.rect.collideRect(rect);
77
78
return this;
79
}
80
}
81
82
//Extend the playable object so that the parent is the sprite
83
gamejs.utils.objects.extend(goal, gamejs.sprite.Sprite);
84