-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshotEntity.java
78 lines (69 loc) · 1.75 KB
/
shotEntity.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
package org.newdawn.spaceinvaders;
/**
* An entity representing a shot fired by the player's ship
*
* @author Kevin Glass
*/
public class ShotEntity extends Entity
{
/** The vertical speed at which the players shot moves */
private double moveSpeed = -300;
/** The game in which this entity exists */
private Game game;
/** True if this shot has been "used", i.e. its hit something */
private boolean used = false;
/**
* Create a new shot from the player
*
* @param game The game in which the shot has been created
* @param sprite The sprite representing this shot
* @param x The initial x location of the shot
* @param y The initial y location of the shot
*/
public ShotEntity(Game game,String sprite,int x,int y)
{
super(sprite,x,y);
this.game = game;
dy = moveSpeed;
}
/**
* Request that this shot moved based on time elapsed
*
* @param delta The time that has elapsed since last move
*/
public void move(long delta)
{
// proceed with normal move
super.move(delta);
// if we shot off the screen, remove ourselfs
if (y < -100)
{
game.removeEntity(this);
}
}
/**
* Notification that this shot has collided with another
* entity
*
* @parma other The other entity with which we've collided
*/
public void collidedWith(Entity other)
{
// prevents double kills, if we've already hit something,
// don't collide
if (used)
{
return;
}
// if we've hit an alien, kill it!
if (other instanceof StudentEntity)
{
// remove the affected entities
game.removeEntity(this);
game.removeEntity(other);
// notify the game that the alien has been killed
game.notifyStudentKilled();
used = true;
}
}
}