Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/algaes-escapade/js/lib/lever.js
1038 views
1
/**
2
* Represents a lever. Levers can be pulled and turned on or off, but must be
3
* held down or they will revert to their off state
4
*
5
* @author David North
6
*/
7
function lever()
8
{
9
lever.prototype.constructor.call(this);
10
11
//Fulfil the requirements of the gamejs.sprite.Sprite object
12
this.image = gamejs.image.load('img/switch.png');
13
this.image.crop( new gamejs.Rect( [0,0], [83,43] ));
14
15
var _size = this.image.getSize();
16
17
/**
18
* @var boolean Whether or the lever is being held down
19
*/
20
var _heldDown = false;
21
22
this.rect = new gamejs.Rect([0, 0], [_size[0], _size[1]]);
23
24
/**
25
* Overrides the setState method of the parent so that the object changes
26
* depending on whether it is on or off
27
*
28
* @param boolean state The state to apply
29
*
30
* @return lever
31
*/
32
this.setState = function( state ){
33
if ( state != this.getState() )
34
{
35
if ( state )
36
{
37
this.image.crop( new gamejs.Rect( [83,0], [83,43] ));
38
}
39
else
40
{
41
this.image.crop( new gamejs.Rect( [0,0], [83,43] ));
42
}
43
}
44
45
//Update the state using the parent setState method
46
lever.prototype.setState.call(this, state);
47
}
48
49
/**
50
* Updates the object, ready for the next draw request
51
*
52
* @param msDuration
53
*
54
* @return lever
55
*/
56
this.update = function( msDuration ){
57
//If the lever isn't held down then turn it off
58
if ( !_heldDown )
59
{
60
this.setState(false);
61
}
62
63
//the default state is to be not held down, unless otherwise told
64
_heldDown = false;
65
66
return this;
67
}
68
69
/**
70
* Handles the collision between a playable and this object
71
*
72
* @param playable playable The playable that collision has happened on
73
*
74
* @return lever
75
*/
76
this.handleCollision = function( playable ){
77
78
if ( _hasCollided(this, playable ) )
79
{
80
//If the player is colliding with this lever then it is held down
81
_heldDown = true;
82
}
83
84
return this;
85
}
86
87
/**
88
* Handles player input. If the player activates the switch then actions
89
* may need to be taken
90
*
91
* @param world world The world this event came from
92
* @param gamejs.Event event The event that fired
93
*/
94
this.handleInput = function(world, event){
95
var playable = world.getPlayer().getCurrentPlayable();
96
97
//Only check the event if a key has been pushed and the player is
98
//colliding with the lever
99
if ( event.type === gamejs.event.KEY_DOWN
100
&& _hasCollided(this, playable) )
101
{
102
//If the key was 'enter' or 'e' then set the new state of the lever
103
switch( event.key )
104
{
105
case gamejs.event.K_e:
106
case gamejs.event.K_ENTER:
107
this.setState( !this.getState() );
108
break;
109
}
110
}
111
}
112
113
var _hasCollided = function ( self, playable )
114
{
115
var _collideRect = new gamejs.Rect(
116
[self.rect.x + 34, self.rect.y],
117
[32 , _size[1]]
118
);
119
120
return _collideRect.collideRect(playable.rect);
121
}
122
}
123
124
//Set the parent of the lever to io
125
include_once(['lib/io.js']);
126
lever.prototype = new io();
127
128