-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mario.java
183 lines (154 loc) · 3.71 KB
/
Mario.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
import java.awt.Graphics;
class Mario extends Sprite {
int previousX;
int previousY;
int solidCount;
int marioSpriteVal;
int jumpCount;
int coinCount;
int xSpeed = 10;
double verticalVelocity;
//////////////////////////////////////
///////////// Constructors ///////////
//////////////////////////////////////
Mario(Model m) {
model = m;
x = 100;
y = 100;
w = 60;
h = 95;
solidCount = 0;
jumpCount = 0;
coinCount = 0;
verticalVelocity = 0;
}
Mario(Model m, Json ob) {
model = m;
x = (int) ob.getLong("x");
y = (int) ob.getLong("y");
w = (int) ob.getLong("w");
h = (int) ob.getLong("h");
solidCount = 0;
}
Mario(Mario that, Model newModel) {
this.model = newModel;
this.x = that.x;
this.y = that.y;
this.w = that.w;
this.h = that.h;
this.previousY = that.previousX;
this.previousY = that.previousY;
this.coinCount = that.coinCount;
this.jumpCount = that.jumpCount;
this.verticalVelocity = that.verticalVelocity;
this.marioSpriteVal = that.marioSpriteVal;
this.solidCount = that.solidCount;
}
//////////////////////////////////////
/////////// Utility Methods //////////
//////////////////////////////////////
public void update() {
verticalVelocity += 5;
y += verticalVelocity;
solidCount++;
if (y > 295) {
verticalVelocity = 0.0;
y = 295; // snap back to the ground
solidCount = 0;
}
generalCollision();
}
void generalCollision() {
for (int i = 0; i < model.sprites.size(); i++) {
Sprite s = model.sprites.get(i);
if ((s.isBrick() || s.isCoinBlock()) && checkCollision(model, this, s)) {
setBarrier(s);
}
}
}
void setBarrier(Sprite b) {
// Left of Brick Barrier
if ((x + model.cameraPos + w >= b.x) && (model.previousCameraPos + previousX < b.x) && (previousY + h > b.y)
&& (previousY < b.y + b.h)) {
model.cameraPos -= xSpeed;
}
// Right of Brick Barrier
if ((model.cameraPos + x <= b.x + b.w) && (previousX + model.previousCameraPos > b.x + b.w)) {
model.cameraPos += xSpeed;
}
// Top of Brick Barrier
if ((y + h >= b.y) && (previousY + h <= b.y)) {
verticalVelocity = 0.0;
y = b.y - h - 1;
solidCount = 0;
}
// Bottom of Brick Barrier
if ((y <= b.y + b.h) && (previousY > b.y + b.h)) {
y = b.y + b.h + 1;
verticalVelocity = 0.0;
if (b.isCoinBlock())
((CoinBlock) b).generateCoin(this);
}
}
@Override
public void draw(Graphics g) {
int j = this.getSpriteVal();
g.drawImage(View.mario_images[j], this.x, this.y, null);
}
@Override
public Sprite cloneSprite(Model m) {
return new Mario(this, m);
}
//////////////////////////////////////
////////// Movement Methods //////////
//////////////////////////////////////
void runRight() {
model.cameraPos += xSpeed;
if (model.isCopy == false)
changeSpriteVal();
}
void runLeft() {
model.cameraPos -= xSpeed;
if (model.isCopy == false)
changeSpriteVal();
}
void jump() {
if (solidCount < 4) {
verticalVelocity -= 15.0;
jumpCount++;
}
}
//////////////////////////////////////
///////// Attribute Getters //////////
//////////////////////////////////////
@Override
public boolean isMario() {
return true;
}
public int getSpriteVal() {
return marioSpriteVal;
}
@Override
public Json marshal() {
Json ob = Json.newObject();
ob.add("type", "mario");
ob.add("x", this.x);
ob.add("y", this.y);
ob.add("w", this.w);
ob.add("h", this.h);
return ob;
}
//////////////////////////////////////
///////// Attribute Setters //////////
//////////////////////////////////////
public void changeSpriteVal() {
marioSpriteVal++;
if (marioSpriteVal == 5)
marioSpriteVal = 0;
}
void setPrevious() {
this.previousX = x;
this.previousY = y;
model.previousCameraPos = model.cameraPos;
}
}