-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.java
More file actions
133 lines (102 loc) · 2.71 KB
/
Entity.java
File metadata and controls
133 lines (102 loc) · 2.71 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* An abstract representation of a game entity.
* @author 216517
*
*/
public abstract class Entity {
private Image image;
private final String fileName;
private Position position;
private boolean dead = false;
private boolean firedBullet;
private double velocity = 0.0;
private double gravity = 0.25;
private int hitpoints;
private Bullet currentBullet = null;
private final Rectangle hitbox;
public Entity(final String fileName, final Position position) {
this.fileName = fileName;
try {
this.image = ImageIO.read(
this.getClass().getClassLoader().getResource("assets/" + fileName + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
this.position = position;
this.hitbox = new Rectangle();
this.dead = false;
}
public abstract void fire(Bullet bullet);
public abstract void update(Graphics2D g);
public void draw(Graphics2D g) {
if (dead) {
return;
}
//g.setColor(Color.GREEN);
//g.drawRect(position.getX(), position.getY(), image.getWidth(null), image.getHeight(null));
g.drawImage(image, position.getX(), position.getY(), null);
}
public boolean collidesWithSprite(Entity entity) {
hitbox.setBounds(position.getX(), position.getY(), image.getWidth(null), image.getHeight(null));
entity.getHitbox().setBounds(entity.getPosition().getX(), entity.getPosition().getY(),
entity.getImage().getWidth(null), entity.getImage().getHeight(null));
return hitbox.intersects(entity.getHitbox()) && !entity.isDead();
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
public String getFileName() {
return fileName;
}
public boolean isDead() {
return dead;
}
public void setDead(boolean dead) {
this.dead = dead;
}
public Image getImage() {
return image;
}
public double getVelocity() {
return velocity;
}
public void setVelocity(double velocity) {
this.velocity = velocity;
}
public double getGravity() {
return gravity;
}
public void setGravity(double gravity) {
this.gravity = gravity;
}
public int getHitpoints() {
return hitpoints;
}
public void setHitpoints(int hitpoints) {
this.hitpoints = hitpoints;
}
public Bullet getCurrentBullet() {
return currentBullet;
}
public void setCurrentBullet(Bullet currentBullet) {
this.currentBullet = currentBullet;
}
public Rectangle getHitbox() {
return hitbox;
}
public boolean isFiredBullet() {
return firedBullet;
}
public void setFiredBullet(boolean firedBullet) {
this.firedBullet = firedBullet;
}
}