Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/algaes-escapade/js/lib/door.js
1037 views
1
/**
2
* Represents a door which can be passed only when the state is set to true
3
*
4
* @author David North
5
*/
6
function door()
7
{
8
door.prototype.constructor.call(this);
9
10
//Set up the variables required by the sprite inheritance
11
this.image = gamejs.image.load('img/door.png');
12
this.image.crop( new gamejs.Rect( [0,0], [83,466] ));
13
14
var _size = this.image.getSize();
15
this.rect = new gamejs.Rect([0, 0], [_size[0], _size[1]]);
16
17
/**
18
* Overrides the setState method of the parent so that the object changes
19
* depending on whether it is on or off
20
*
21
* @param boolean state The state to apply
22
*
23
* @return door
24
*/
25
this.setState = function( state ){
26
//Only update if the state has actually changed
27
if ( state != this.getState() )
28
{
29
if ( state )
30
{
31
this.image.crop( new gamejs.Rect( [83,0], [83,466] ));
32
}
33
else
34
{
35
this.image.crop( new gamejs.Rect( [0,0], [83,466] ));
36
}
37
}
38
39
//Update the state using the parent setState method
40
return door.prototype.setState.call(this, state);
41
}
42
43
/**
44
* Handles the collision between a playable and this object
45
*
46
* @return door
47
*/
48
this.handleCollision = function( playable ){
49
50
if ( !this.getState() )
51
{
52
//Modify the rectangle. The player shouldn't hit the door
53
//until they are at the beam
54
var targetX = this.rect.x + 26;
55
var targetY = this.rect.y;
56
57
var targetWidth = this.rect.width - 52;
58
var targetHeight = this.rect.height;
59
60
var rect = new gamejs.Rect(
61
[targetX, targetY], [targetWidth, targetHeight]
62
);
63
64
playerCollides(playable, rect);
65
}
66
67
return this;
68
}
69
}
70
71
//Set the parent of the door to io
72
include_once(['lib/io.js']);
73
door.prototype = new io();
74
75