-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollisionManager.java
106 lines (98 loc) · 3.03 KB
/
CollisionManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.util.ArrayList;
/**
* This class handles all collisions among sprites.
*
* @author John Kurlak (kurlak)
* Weston Thayer (weston5)
* Panhavorn Hok (onehok1)
* @version 12.07.09
*/
public class CollisionManager
{
/**
* This method detects whether the first sprite is overtop of the second
* sprite.
*
* @param one The first sprite
* @param two The second sprite
* @return Returns true if the two sprites are overtop of each other
* and false if they are not
*/
public static boolean detectCollision(Sprite one, Sprite two)
{
// To prevent errors concerning concurrent modification
if (one != null && two != null)
{
// X coordinate detection
int oneLeft = one.getX();
int oneRight = one.getX() + one.getWidth();
int twoLeft = two.getX();
int twoRight = two.getX() + two.getWidth();
boolean overLeftSide = (oneRight >= twoLeft) && (oneRight <=
twoRight);
boolean overRightSide = (oneLeft <= twoRight) && (oneLeft >=
twoLeft);
boolean overHoriz = (oneLeft <= twoLeft && oneRight
>= twoRight);
// Y coordinate detection
int oneTop = one.getY();
int oneBottom = one.getY() + one.getHeight();
int twoTop = two.getY();
int twoBottom = two.getY() + two.getHeight();
boolean overTopSide = (oneBottom >= twoTop) && (oneBottom <=
twoBottom);
boolean overBottomSide = (oneTop <= twoBottom) && (oneTop >=
twoTop);
boolean overVert = (oneTop <= twoTop && oneBottom >=
twoBottom);
if ((overLeftSide || overRightSide || overHoriz) &&
(overTopSide || overBottomSide || overVert))
{
return true;
}
}
return false;
}
/**
* This method checks to see if there are collisions among sprites and calls
* their respective collision() methods if there is.
*/
public static void handleCollisions()
{
ArrayList<Sprite> sprites = GamePanel.getSprites();
// Loop through all of the sprites
for (int i = 0; i < sprites.size(); ++i)
{
Sprite one = sprites.get(i);
// Only compare sprites with which we are concerned
if ((one instanceof Collidable && one.isAlive()) && (one instanceof
KillableSprite || one instanceof Weapon || one instanceof
PowerUp))
{
for (int j = 0; j < sprites.size(); ++j)
{
try
{
Sprite two = sprites.get(j);
// If one is a Player, Enemy, Weapon, or PowerUp and two
// is not the same and there is a collision
if (((one instanceof Player && !(two instanceof Player))
|| (one instanceof Enemy && !(two instanceof Enemy))
|| (one instanceof Weapon && !(two instanceof
Weapon)) || (one instanceof PowerUp && !(two
instanceof PowerUp))) && detectCollision(one, two))
{
Collidable collided = (Collidable) one;
collided.collision(two);
}
}
catch (IndexOutOfBoundsException e)
{
// Happens due to concurrency issues... and can be
// ignored
}
}
}
}
}
}