-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameObject.java
184 lines (147 loc) · 3.98 KB
/
GameObject.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class GameObject implements ScrollObject{
private int objectX, objectY;
private static double scrollConst = .95;
private BufferedImage objectImg;
private String fileName;
private Rectangle hitBox;
private int height,length;
public GameObject(int objectX, int objectY, String fileName) {
super();
this.objectX = objectX;
this.objectY = objectY;
this.fileName = fileName;
try {
objectImg = ImageIO.read(new File(fileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
hitBox = new Rectangle(objectX, objectY, objectImg.getWidth(), objectImg.getHeight());
}
public boolean isTouchingPlayer(Player p) {
if (
hitBox.x < p.hitBox.x + p.hitBox.width &&
hitBox.x + hitBox.width > p.hitBox.x &&
hitBox.y < p.hitBox.y + p.hitBox.height &&
hitBox.height + hitBox.y > p.hitBox.y
) {
// Collision detected!
return true;
}
else if( objectX < p.x + p.playerWidth &&
objectX + objectImg.getWidth() > p.x &&
objectY < p.y + p.playerHeight &&
objectImg.getHeight() + objectY > p.y)
return true;
else {
// No collision
return false;
}
}
public boolean letGo(Player p) {
if(isTouchingPlayer(p)&&p.direction=="right")
{
if(!p.direction.equals("right")) {
return true;
}
}
else if(isTouchingPlayer(p)&&p.direction=="left")
{
if(!p.direction.equals("left")) {
return true;
}
}
else if(isTouchingPlayer(p)&&p.direction=="up")
{
if(!p.direction.equals("up")) {
return true;
}
}
else if(isTouchingPlayer(p)&&p.direction=="down")
{
if(!p.direction.equals("down")) {
return true;
}
}
return false;
}
/*
if(hitBox.intersects(p.hitBox)) {
if(p.direction.equals("left")) {
//p.velx = 0;
//p.x += 2;
return true;
}
else if (p.direction.equals("right")){
//p.velx = 0;
//p.x += -2;
return true;
}
else {
//p.x = p.x;
}
}
return false; */
public void drawObject(Graphics2D g2d) {
g2d.drawImage(objectImg, objectX, objectY, null);
g2d.draw(hitBox);
}
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = dimg.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return dimg;
}
@Override
public void update(Player p) {
if(isTouchingPlayer(p)) {
if(letGo(p)) {
objectX += -(p.getXVel());
objectY += -(p.getYVel());
hitBox = new Rectangle(objectX, objectY, objectImg.getWidth(), objectImg.getHeight());
}else {
objectX += (p.getXVel());
objectY += (p.getYVel());
hitBox = new Rectangle(objectX, objectY, objectImg.getWidth(), objectImg.getHeight());
}
}
else {
objectX += -(p.getXVel());
objectY += -(p.getYVel());
hitBox = new Rectangle(objectX, objectY, objectImg.getWidth(), objectImg.getHeight());
}
}
public int getObjectX() {
return objectX;
}
public void setObjectX(int objectX) {
this.objectX = objectX;
}
public int getObjectY() {
return objectY;
}
public void setObjectY(int objectY) {
this.objectY = objectY;
}
public BufferedImage getObjectImg() {
return objectImg;
}
public void setObjectImg(BufferedImage objectImg) {
this.objectImg = objectImg;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}