Skip to content

Commit 8177ffc

Browse files
committed
updated colision and player movement code
1 parent 88ecfe5 commit 8177ffc

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/com/redomar/game/entities/Mob.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,16 @@ public void move(int xa, int ya) {
5656
movingDir = 3;
5757
}
5858

59-
if (!hasCollided(xa, ya)) {
60-
setX(getX() + xa * (int) speed);
61-
setY(getY() + ya * (int) speed);
59+
for (int x = 0; x < Math.abs(xa); x++) {
60+
if (!hasCollided(abs(xa), ya)) {
61+
setX(getX() + abs(xa) * (int) speed);
62+
}
63+
}
64+
65+
for (int y = 0; y < Math.abs(ya); y++) {
66+
if (!hasCollided(xa, abs(ya))) {
67+
setY(getY() + abs(ya) * (int) speed);
68+
}
6269
}
6370
}
6471

@@ -94,6 +101,25 @@ public boolean hasCollided(int xa, int ya){
94101

95102
return false;
96103
}
104+
105+
public boolean hasCollidedAlt(int xa, int ya){
106+
boolean solid = false;
107+
for (int c = 0; c < 4; c++) {
108+
int xt = ((x + xa) - c % 2 * 8) / 8;
109+
int yt = ((y + ya) - c / 2 * 8) / 8;
110+
int ix = (int) Math.ceil(xt);
111+
int iy = (int) Math.ceil(yt);
112+
if (c % 2 == 0) ix = (int) Math.floor(xt);
113+
if (c / 2 == 0) iy = (int) Math.floor(yt);
114+
if(level.getTile(ix, iy).isSolid()) solid = true;
115+
}
116+
return solid;
117+
}
118+
119+
private int abs(int i){
120+
if (i < 0) return -1;
121+
return 1;
122+
}
97123

98124
protected boolean isSolid(int xa, int ya, int x, int y) {
99125

src/com/redomar/game/entities/Player.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class Player extends Mob {
1717
private static Name customeName = new Name();
1818
private Swim swim;
1919

20+
private static double speed = 1;
21+
2022
private int colour, shirtCol, faceCol;
2123
private int tickCount = 0;
2224
private String userName;
@@ -27,7 +29,7 @@ public class Player extends Mob {
2729

2830
public Player(LevelHandler level, int x, int y, InputHandler input,
2931
String userName, int shirtCol, int faceCol) {
30-
super(level, "Player", x, y, 1);
32+
super(level, "Player", x, y, speed);
3133
this.input = input;
3234
this.userName = userName;
3335
this.faceCol = faceCol;
@@ -188,4 +190,12 @@ public void setSwim(Swim swim) {
188190
this.swim = swim;
189191
}
190192

193+
public static double getSpeed() {
194+
return speed;
195+
}
196+
197+
public static void setSpeed(double speed) {
198+
Player.speed = speed;
199+
}
200+
191201
}

0 commit comments

Comments
 (0)