-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.java
More file actions
185 lines (143 loc) · 3.71 KB
/
GameObject.java
File metadata and controls
185 lines (143 loc) · 3.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
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
185
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
public abstract class GameObject implements MovingObject {
private double speed = 0;// 0 - 10
private double direction = Math.PI / 2, // degrees or radians
x= 0, y= 0, // >= 0
size = 10, // 10 might be a good size
power = 10;// not sure about this...
private int level = 1;//
private Color color;
private Image object;
private Image square, triangle, pentagon;
private BufferedImage tank;
private double damage, movingDirection;
private Rectangle rect;
private String name;
private int health = 100;
public GameObject(Color color, double x, double y, double size) {
this.color = color;
this.x = x;
this.y = y;
this.size = size;
this.damage = 10;
rect = new Rectangle();
// assign damage
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setHealth(int health){
this.health = health;
}
public int getHealth(){
return this.health;
}
public void setDir(double d){
setDirection(d);
}
public double getDirection(){
return direction;
}
public void setDirection(double direction) {
this.direction = direction;
}
public void setMovingDirection(double movingDirection){
this.movingDirection = movingDirection;
}
public double getMovingDirection(){
return this.movingDirection;
}
public double getX(){
return x;
}
public void setX(int i){
x = i;
}
public double getY(){
return y;
}
public void setY(int i){
y = i;
}
public double getSize(){
return size;
}
@Override
public void move() {
x+= getSpeed()*Math.cos(getMovingDirection());
y+= getSpeed()*Math.sin(getMovingDirection());
if(this instanceof Bullet){
//System.out.println(this);
}
checkOffScreen();
// maybe "push" back onto the screen change direction if
// this object goes off the screen
}
private void openImage() {
try {
URL url = getClass().getResource("images/tank.png");
tank = ImageIO.read(url);
} catch (Exception e) {
System.out.println("problem opening the tank");
e.printStackTrace();
}
try {
URL url = getClass().getResource("images/square.png");
square = ImageIO.read(url);
} catch (Exception e) {
System.out.println("problem opening the square");
e.printStackTrace();
}
try {
URL url = getClass().getResource("images/triangle.png");
triangle = ImageIO.read(url);
} catch (Exception e) {
System.out.println("problem opening the triangle");
e.printStackTrace();
}
try {
URL url = getClass().getResource("images/hexagon.png");
pentagon = ImageIO.read(url);
} catch (Exception e) {
System.out.println("problem opening the hexagon");
e.printStackTrace();
}
}
public abstract void checkOffScreen();
public abstract void draw(Graphics g);
@Override
public Rectangle getBoundingRect() {
return this.rect;
}
public void setBoundingRect(int x, int y, int width, int height){
rect.setBounds(x, y, width, height);
}
public void takeDamage(GameObject go){
this.health -= go.getDamage();
}
private double getDamage() {
// TODO Auto-generated method stub
return this.damage;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public String toString(){
String s = "";
s+=this.getClass();
s+=this.getX()+" , "+this.getY()+" speed: "+this.getSpeed()+" dir: "+this.getDirection();
return s;
}
}