-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBullet.java
More file actions
175 lines (137 loc) · 3.75 KB
/
Copy pathBullet.java
File metadata and controls
175 lines (137 loc) · 3.75 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
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
public class Bullet implements Enemy{
private static final int XSIZE = 52; // width of the image
private static final int YSIZE = 8; // height of the image
//private static final int DX = 2; // amount of pixels to move in one update
private static final int YPOS = 150; // vertical position of the image
private int x;
private int y;
private int dx;
private int originalX;
private int count;
private Hunter player;
private TileMap2 map;
private Image spriteImage; // image for sprite
private Image spriteLeftImage;
private Image spriteRightImage;
private boolean fired;
private int direction;
public Bullet(TileMap2 map, int direction, int x, int y){
this.map = map;
this.player = map.getPlayer();
fired = true;
this.direction = direction;
this.x = x;
this.y = y;
dx = 15;
count = 0;
spriteLeftImage = ImageManager2.loadImage("images2/character/bulletLeft52x11.png");
spriteRightImage = ImageManager2.loadImage("images2/character/bulletRight52x11.png");
if(direction ==1){
spriteImage = spriteLeftImage;
}else{
spriteImage = spriteRightImage;
}
}
@Override
public void draw(Graphics2D g2) {
g2.drawImage(spriteImage, x, y, XSIZE, YSIZE, null);
}
public boolean collidesWithPlayer () {
Rectangle2D.Double myRect = getBounds();
Rectangle2D.Double playerRect = player.getBounds();
if (myRect.intersects(playerRect)) {
System.out.println ("Collision with player!");
return true;
}
else
return false;
}
@Override
public Double getBounds() {
return new Rectangle2D.Double (x, y - spriteImage.getHeight(null)+115, spriteImage.getWidth(null), spriteImage.getHeight(null));
}
@Override
public void update() {
count++;
if (count > 20)
fired = false;
if(direction == 1){
//code for left arrow
Point tilePos = collidesWithTile((x-dx), y);
if(tilePos != null)
fired = false;
x = x - dx;
}else{
//code for right arrow
Point tilePos = collidesWithTile((x+dx+ spriteImage.getWidth(null)), y);
if(tilePos != null)
fired = false;
x = x + dx;
}
}
@Override
public int getWidth() {
return 52;
}
@Override
public void setX(int i) {
this.x = i;
}
@Override
public int getHeight() {
return 8;
}
@Override
public void setY(int i) {
this.y = i;
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
@Override
public Image getImage() {
return spriteImage;
}
@Override
public void wakeUp() {
// peewooo
return;
}
@Override
public void setOriginalX(int i) {
this.originalX = x;
}
public Bullet clone(){
return this;
}
public boolean fired(){
return fired;
}
public void setFired(){
fired = false;
return;
}
public Point collidesWithTile(int newX, int newY) {
//int playerWidth = spriteImage.getWidth(null);
int offsetY = map.getOffsetY();
int xTile = map.pixelsToTiles(newX);
int yTile = map.pixelsToTiles(newY - offsetY);
if (map.getTile(xTile, yTile) != null) {
Point tilePos = new Point (xTile, yTile);
return tilePos;
}
else {
return null;
}
}
}