-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.java
More file actions
181 lines (147 loc) · 3.99 KB
/
Copy pathSprite.java
File metadata and controls
181 lines (147 loc) · 3.99 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
import java.awt.Graphics2D;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Sprite
{
BufferedImage[] animations;
protected int x;
protected int xLoc;
protected int y;
protected int yLoc;
private int height;
protected int heightLoc;
private int width;
protected int widthLoc;
protected int[] states = new int[]{0,0,0,0};
private LocationGrid grid;
private int gridIndex;
protected int currentImageIndex;
public Sprite(int x, int y, String file, LocationGrid g, int index){
animations = SpriteAnimations.createSprite(file);
this.x = x;
xLoc = x;
this.y = y;
yLoc = y;
width = animations[0].getWidth();
widthLoc = width;
height = animations[0].getHeight();
heightLoc = height;
grid = g;
gridIndex = index;
grid.update(this);
currentImageIndex = 0;
}
public BufferedImage appear(BufferedImage b){
final BufferedImage finalImage = new BufferedImage(b.getWidth(), b.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = finalImage.createGraphics();
g.drawImage(b,0,0,null);
g.drawImage(animations[currentImageIndex],x,y, null);
g.dispose();
return finalImage;
}
public void left(BufferedImage b){
states[0] = 0;
states[2] = 0;
states[3] = 0;
if (x > 10) moveX(-10);
grid.update(this);
if (grid.checkCollision(gridIndex)) {
moveX(10);
grid.update(this);
}
currentImageIndex = 4+states[1];
states[1] = (states[1]+1)%4;
}
public void right(BufferedImage b){
states[0] = 0;
states[1] = 0;
states[3] = 0;
if (x < b.getWidth()-getWidth()-10) moveX(10);
grid.update(this);
if (grid.checkCollision(gridIndex)) {
moveX(-10);
grid.update(this);
}
currentImageIndex = 8+states[2];
states[2] = (states[2]+1)%4;
}
public void down(BufferedImage b){
states[1] = 0;
states[2] = 0;
states[3] = 0;
moveY(10);
if (y >= b.getHeight()) {
y = 0;
yLoc = y;
}
grid.update(this);
if (grid.checkCollision(gridIndex)) {
moveY(-10);
if (y < 0) {
y = b.getHeight()-10;
yLoc = y;
}
grid.update(this);
}
currentImageIndex = states[0];
states[0] = (states[0]+1)%4;
}
public void up(BufferedImage b){
states[0] = 0;
states[1] = 0;
states[2] = 0;
moveY(-10);
if (y < 0) {
y = b.getHeight();
yLoc = y;
}
grid.update(this);
if (grid.checkCollision(gridIndex)) {
moveY(10);
if (y > b.getHeight()) {
y = 10;
yLoc = y;
}
grid.update(this);
}
currentImageIndex = 12+states[3];
states[3] = (states[3]+1)%4;
}
public void moveY(int num){
y += num;
yLoc += num;
}
public void moveX(int num){
x += num;
xLoc += num;
}
public int getX(){
return x;
}
public int getXLoc(){
return xLoc;
}
public int getY(){
return y;
}
public int getYLoc(){
return yLoc;
}
public int getHeight(){
return height;
}
public int getHeightLoc(){
return heightLoc;
}
public int getWidth(){
return width;
}
public int getWidthLoc(){
return widthLoc;
}
public int getIndex(){
return gridIndex;
}
}