-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.java
More file actions
111 lines (88 loc) · 3.19 KB
/
Entity.java
File metadata and controls
111 lines (88 loc) · 3.19 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
//An Entity represents some indivdual thing that is drawn to the game window.
//Everything drawn and animated in the game window is an Entity, including the player.
//
//Each time the game window "refreshes" all entities in the game's display list
//are drawn according to their respective attributes.
public abstract class Entity {
//Location of image file to be drawn for the Entity
private String imageName;
//The height and width of the entity (in pixels) to be drawn to the game window
//Note that all Entities are ultimately drawn as rectangles in the game window.
//An entity's image will be stretched to fit its rectangle per its height/width
//This rectangle is also its "hitbox", governing its boundaries when determining collisions.
private int height, width;
//The x and y coordinate of the Entity to be drawn in the game window.
protected int x, y;
//Determines if the Entity is visible in the game window or not
private boolean isVisible;
public Entity(int x, int y, int width, int height, String imageName){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.imageName = imageName;
this.isVisible = true; //by default, all newly instantiated Entities are visible
}
//Set and retrieve this entity's coordinates
public int getX(){
return x;
}
public void setX(int newX){
this.x = newX;
}
public int getY(){
return y;
}
public void setY(int newY){
this.y = newY;
}
//Set and retrieve this entity's dimensions
public int getHeight(){
return height;
}
public void setHeight(int newHeight){
this.height = newHeight;
}
public int getWidth(){
return width;
}
public void setWidth(int newWidth){
this.width = newWidth;
}
//Set and retrieve this entity's image and visibility status
public String getImageName(){
return imageName;
}
public void setImageName(String newImageName){
this.imageName = newImageName;
}
public void setVisible(boolean isVisible){
this.isVisible = isVisible;
}
public boolean isVisible(){
return isVisible;
}
//Checks to see if this Entity is colliding with the argument Entity
//Meaning any part of the two Entities are overlapping
public boolean isCollidingWith(Entity other){
int thisX = this.x + this.width;
int thisY = this.y + this.height;
if(other.getY()+ other.getHeight() < this.y)
return false;
else if(other.getY() > thisY)
return false;
else if(other.getX()+ other.getWidth() < this.x)
return false;
else if(other.getX() > thisX)
return false;
return true;
}
//Checks to see if this Entity if argument x,y coordinate is inside
//the referenced Entity
public boolean containsPoint(int x, int y){
if (this.x <= x && this.x + this.width >= x){
return (this.y <= y && this.y + this.height >= y);
}
return false;
}
}