-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPot.java
More file actions
139 lines (121 loc) · 2.8 KB
/
Pot.java
File metadata and controls
139 lines (121 loc) · 2.8 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
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Pot extends Sprite {
//Member variables
static BufferedImage images[];
int maxImageNum = 2;
int animationNum;
int speed = 5;
boolean inOnePiece;
int xdirection, ydirection;
boolean moveUp;
boolean moveRight;
boolean moveDown;
boolean moveLeft;
int countdown = 15;
Pot(int locationX, int locationY){
this.x = locationX;
this.y = locationY;
isActive = true;
loadImage();
w = 35;
h = 35;
inOnePiece = true;
}
void cycleImages(){
if(inOnePiece){
animationNum = maxImageNum - maxImageNum;
}
else{
animationNum = maxImageNum - 1;
}
}
//Pot movement
void movePotUp(){
xdirection = 0;
ydirection = -1;
this.y += speed * ydirection;
}
void movePotRight(){
xdirection = 1;
ydirection = 0;
this.x += speed * xdirection;
}
void movePotDown(){
xdirection = 0;
ydirection = 1;
this.y += speed * ydirection;
}
void movePotLeft(){
xdirection = -1;
ydirection = 0;
this.x += speed * xdirection;
}
//Checking collision with pots
@Override
void Collided(){
inOnePiece = false;
speed = 0;
}
@Override
void draw(Graphics g){
g.drawImage(images[animationNum], x - View.scrollPositonX, y - View.scrollPositonY, null);
}
//Going to use lazy loading
@Override
void loadImage(){
if (images == null){
images = new BufferedImage[maxImageNum];
for (int i = 0; i < maxImageNum; i++){
String tmp = "images/pot" + (i + 1) + ".png";
images[i] = View.loadImage(tmp);
}
}
}
@Override
boolean update(){
//Pot movement with link
if (moveUp){
movePotUp();
}
if (moveDown){
movePotDown();
}
if (moveLeft){
movePotLeft();
}
if (moveRight){
movePotRight();
}
cycleImages();
if (!inOnePiece){
countdown--;
if (countdown == 0){
isActive = false;
}
}
return isActive;
}
@Override
Json Marshal(){
Json ob = Json.newObject();
ob.add("potx", x);
ob.add("poty", y);
return ob;
}
Pot(Json ob){
w = 35;
h = 35;
x = (int)ob.getLong("potx");
y = (int)ob.getLong("poty");
loadImage();
inOnePiece = true;
}
@Override
boolean isPot(){return true;}
@Override
public String toString()
{
return "Pot (x,y) = (" + x + ", " + y + ")";
}
}