Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/algaes-escapade/js/lib/utils.js
1036 views
1
function include_once(includes)
2
{
3
if ( typeof($('head').data('included')) == 'undefined' )
4
{
5
$('head').data('included', []);
6
}
7
8
for ( var i = 0; i < includes.length; i++ )
9
{
10
if ( $.inArray(includes[i], $('head').data('included')) === -1 )
11
{
12
$('head').append(
13
'<script type="text/javascript" src="js/'+includes[i]+'"></script>'
14
);
15
16
$('head').data('included').push(includes[i]);
17
}
18
}
19
}
20
21
function playerCollides(playable, rect, drag)
22
{
23
if ( typeof(drag) == 'undefined' )
24
{
25
drag = 50;
26
}
27
28
//Define the top edge (left to right, along the top of the
29
//colliding block)
30
var topEdge = [ [rect.left, rect.top], [rect.right, rect.top] ];
31
32
//Define the top edge (left to right, along the bottom of the
33
//colliding this)
34
var bottomEdge = [ [rect.left, rect.bottom], [rect.right, rect.bottom] ];
35
36
//Define the left edge (top to bottom, along the left of the
37
//colliding this)
38
var leftEdge = [ [rect.left, rect.top], [rect.left, rect.bottom] ];
39
40
//Define the right edge (top to bottom, along the right of the
41
//colliding this)
42
var rightEdge = [ [rect.right, rect.top], [rect.right, rect.bottom] ];
43
44
//Check the top and bottom colliision points. If a collision is
45
//detected then set the velocity on the Y axis to zero and move
46
//the playable so that it is no longer colliding
47
if ( playable.rect.collideLine(topEdge[0], topEdge[1]) )
48
{
49
var newX = playable.getVelocity().x;
50
if (newX < 0 )
51
{
52
newX += drag;
53
}
54
else if ( newX > 0 )
55
{
56
newX -= drag;
57
}
58
playable.setVelocity( newX, 0 );
59
playable.rect.bottom = (rect.top - 0.01);
60
playable.setMovement('walk');
61
}
62
else if ( playable.rect.collideLine(bottomEdge[0], bottomEdge[1]) )
63
{
64
playable.setVelocity( playable.getVelocity().x, 0 );
65
playable.rect.top = (rect.bottom + 0.01);
66
}
67
68
//Check the left and right colliision points. If a collision is
69
//detected then set the velocity on the X axis to zero and move
70
//the playable so that it is no longer colliding
71
if ( playable.rect.collideLine(leftEdge[0], leftEdge[1]) )
72
{
73
playable.setVelocity( 0, playable.getVelocity().y );
74
playable.rect.right = (rect.left - 0.01);
75
76
}
77
else if ( playable.rect.collideLine(rightEdge[0], rightEdge[1]) )
78
{
79
playable.setVelocity( 0, playable.getVelocity().y );
80
playable.rect.left = (rect.right + 0.01);
81
}
82
}
83