Skip to content

Commit a90cdf4

Browse files
committed
merge with aside
2 parents 7f748d2 + 610ac58 commit a90cdf4

File tree

7 files changed

+95
-87
lines changed

7 files changed

+95
-87
lines changed

src/com/redomar/game/Game.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void init() {
111111
setWindow(new WindowHandler(this));
112112
setMap("/levels/custom_level.png");
113113
setMap(1);
114-
Packet00Login loginPacket = new Packet00Login(player.getUsername(), player.x, player.y);
114+
Packet00Login loginPacket = new Packet00Login(player.getUsername(), player.getX(), player.getY());
115115

116116
if (socketServer != null) {
117117
socketServer.addConnection((PlayerMP) getPlayer(), loginPacket);
@@ -205,8 +205,8 @@ public void render() {
205205
return;
206206
}
207207

208-
int xOffset = getPlayer().x - (screen.getWidth() / 2);
209-
int yOffset = getPlayer().y - (screen.getHeight() / 2);
208+
int xOffset = getPlayer().getX() - (screen.getWidth() / 2);
209+
int yOffset = getPlayer().getY() - (screen.getHeight() / 2);
210210

211211
getLevel().renderTiles(screen, xOffset, yOffset);
212212

Lines changed: 37 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.redomar.game.entities;
22

3-
import javax.swing.JOptionPane;
4-
53
import com.redomar.game.Game;
64
import com.redomar.game.gfx.Colours;
75
import com.redomar.game.gfx.Screen;
@@ -11,83 +9,25 @@ public class Dummy extends Mob{
119

1210
private int colour, shirtCol, faceCol; //= Colours.get(-1, 111, 240, 310);
1311
private int tickCount = 0;
14-
private int tick = 0;
1512
private int xa = 0;
1613
private int ya = 0;
17-
private int xes = 0;
1814

1915
public Dummy(LevelHandler level, String name, int x, int y, int shirtCol, int faceCol) {
2016
super(level, "h", x, y, 1);
2117
this.faceCol = faceCol;
2218
this.shirtCol = shirtCol;
2319
this.colour = Colours.get(-1, 111, shirtCol, faceCol);
2420
}
25-
26-
public boolean hasCollided(int xa, int ya) {
27-
int xMin = 0;
28-
int xMax = 7;
29-
int yMin = 3;
30-
int yMax = 7;
31-
32-
for (int x = xMin; x < xMax; x++) {
33-
if (isSolid(xa, ya, x, yMin)) {
34-
return true;
35-
}
36-
}
37-
38-
for (int x = xMin; x < xMax; x++) {
39-
if (isSolid(xa, ya, x, yMax)) {
40-
return true;
41-
}
42-
}
43-
44-
for (int y = yMin; y < yMax; y++) {
45-
if (isSolid(xa, ya, xMin, y)) {
46-
return true;
47-
}
48-
}
49-
50-
for (int y = yMin; y < yMax; y++) {
51-
if (isSolid(xa, ya, xMax, y)) {
52-
return true;
53-
}
54-
}
55-
56-
return false;
57-
}
5821

5922
public void tick() {
60-
tick++;
61-
if(tick % (random.nextInt(50) +30) == 0){
62-
xa = random.nextInt(3) -1;
63-
ya = random.nextInt(3) -1;
64-
if(random.nextInt(4) == 0){
65-
xa = 0;
66-
ya = 0;
67-
}
68-
}
6923

70-
if(Game.getPlayer().x == x && Game.getPlayer().y == y && xes == 0){
71-
JOptionPane.showMessageDialog(Game.getGame(), "Zombie Eat You @"+x+" "+y, "Zombie", 1);
72-
xes++;
73-
}
74-
75-
if (xa != 0 || ya != 0) {
76-
move(xa, ya);
77-
isMoving = true;
78-
79-
// Packet02Move packet = new Packet02Move(this.getUsername(), this.x, this.y, this.numSteps, this.isMoving, this.movingDir);
80-
// packet.writeData(Game.getGame().getSocketClient());
81-
82-
} else {
83-
isMoving = false;
84-
}
24+
followMovementAI(getX(), getY(), Game.getPlayer().getX(), Game.getPlayer().getY(), xa, ya, this);
8525

86-
if (level.getTile(this.x >> 3, this.y >> 3).getId() == 4) {
26+
if (level.getTile(this.getX() >> 3, this.getY() >> 3).getId() == 4) {
8727
isSwimming = true;
8828
}
8929

90-
if (isSwimming && level.getTile(this.x >> 3, this.y >> 3).getId() != 4) {
30+
if (isSwimming && level.getTile(this.getX() >> 3, this.getY() >> 3).getId() != 4) {
9131
isSwimming = false;
9232
}
9333

@@ -110,8 +50,8 @@ public void render(Screen screen) {
11050
}
11151

11252
int modifier = 8 * scale;
113-
int xOffset = x - modifier / 2;
114-
int yOffset = y - modifier / 2 - 4;
53+
int xOffset = getX() - modifier / 2;
54+
int yOffset = getY() - modifier / 2 - 4;
11555

11656
if (isSwimming) {
11757
int waterColour = 0;
@@ -152,4 +92,36 @@ public void render(Screen screen) {
15292
}
15393
}
15494

95+
public boolean hasCollided(int xa, int ya) {
96+
int xMin = 0;
97+
int xMax = 7;
98+
int yMin = 3;
99+
int yMax = 7;
100+
101+
for (int x = xMin; x < xMax; x++) {
102+
if (isSolid(xa, ya, x, yMin)) {
103+
return true;
104+
}
105+
}
106+
107+
for (int x = xMin; x < xMax; x++) {
108+
if (isSolid(xa, ya, x, yMax)) {
109+
return true;
110+
}
111+
}
112+
113+
for (int y = yMin; y < yMax; y++) {
114+
if (isSolid(xa, ya, xMin, y)) {
115+
return true;
116+
}
117+
}
118+
119+
for (int y = yMin; y < yMax; y++) {
120+
if (isSolid(xa, ya, xMax, y)) {
121+
return true;
122+
}
123+
}
124+
125+
return false;
126+
}
155127
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public abstract class Entity {
77

8-
public int x, y;
8+
protected int x, y;
99
protected LevelHandler level;
1010

1111
public Entity(LevelHandler level) {
@@ -19,4 +19,20 @@ public final void init(LevelHandler level) {
1919
public abstract void tick();
2020

2121
public abstract void render(Screen screen);
22+
23+
public int getX() {
24+
return x;
25+
}
26+
27+
public void setX(int x) {
28+
this.x = x;
29+
}
30+
31+
public int getY() {
32+
return y;
33+
}
34+
35+
public void setY(int y) {
36+
this.y = y;
37+
}
2238
}

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ public abstract class Mob extends Entity {
1616
protected int scale = 1;
1717
protected boolean isSwimming = false;
1818
protected boolean changeLevels = false;
19+
protected int ticker;
1920

2021
public Mob(LevelHandler level, String name, int x, int y, int speed) {
2122
super(level);
2223
this.name = name;
23-
this.x = x;
24-
this.y = y;
24+
this.setX(x);
25+
this.setY(y);
2526
this.speed = speed;
2627
}
2728

@@ -46,8 +47,8 @@ public void move(int xa, int ya) {
4647
if (xa > 0) {
4748
movingDir = 3;
4849
}
49-
x += xa * speed;
50-
y += ya * speed;
50+
setX(getX() + xa * speed);
51+
setY(getY() + ya * speed);
5152
}
5253
}
5354

@@ -59,16 +60,35 @@ protected boolean isSolid(int xa, int ya, int x, int y) {
5960
return false;
6061
}
6162

62-
Tile lastTile = level.getTile((this.x + x) >> 3, (this.y + y) >> 3);
63-
Tile newtTile = level.getTile((this.x + x + xa) >> 3,
64-
(this.y + y + ya) >> 3);
63+
Tile lastTile = level.getTile((this.getX() + x) >> 3, (this.getY() + y) >> 3);
64+
Tile newtTile = level.getTile((this.getX() + x + xa) >> 3,
65+
(this.getY() + y + ya) >> 3);
6566

6667
if (!lastTile.equals(newtTile) && newtTile.isSolid()) {
6768
return true;
6869
}
6970

7071
return false;
7172
}
73+
74+
protected void followMovementAI(int x, int y, int px, int py, int xa, int ya, Mob mob){
75+
ya = 0;
76+
xa = 0;
77+
if(px > x) xa++;
78+
if(px < x) xa--;
79+
if(py > y) ya++;
80+
if(py < y) ya--;
81+
moveMob(xa, ya, mob);
82+
}
83+
84+
protected void moveMob(int xa, int ya, Mob mob){
85+
if (xa != 0 || ya != 0) {
86+
mob.move(xa, ya);
87+
mob.isMoving = true;
88+
} else {
89+
mob.isMoving = false;
90+
}
91+
}
7292

7393
public String getName() {
7494
return name;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,22 @@ public void tick() {
5050
move(xa, ya);
5151
isMoving = true;
5252

53-
Packet02Move packet = new Packet02Move(this.getUsername(), this.x, this.y, this.numSteps, this.isMoving, this.movingDir);
53+
Packet02Move packet = new Packet02Move(this.getUsername(), this.getX(), this.getY(), this.numSteps, this.isMoving, this.movingDir);
5454
packet.writeData(Game.getGame().getSocketClient());
5555

5656
} else {
5757
isMoving = false;
5858
}
5959

60-
if (level.getTile(this.x >> 3, this.y >> 3).getId() == 4) {
60+
if (level.getTile(this.getX() >> 3, this.getY() >> 3).getId() == 4) {
6161
isSwimming = true;
6262
}
6363

64-
if (isSwimming && level.getTile(this.x >> 3, this.y >> 3).getId() != 4) {
64+
if (isSwimming && level.getTile(this.getX() >> 3, this.getY() >> 3).getId() != 4) {
6565
isSwimming = false;
6666
}
6767

68-
if (level.getTile(this.x >> 3, this.y >> 3).getId() == 11){
68+
if (level.getTile(this.getX() >> 3, this.getY() >> 3).getId() == 11){
6969
changeLevels = true;
7070
}
7171

@@ -87,8 +87,8 @@ public void render(Screen screen) {
8787
}
8888

8989
int modifier = 8 * scale;
90-
int xOffset = x - modifier / 2;
91-
int yOffset = y - modifier / 2 - 4;
90+
int xOffset = getX() - modifier / 2;
91+
int yOffset = getY() - modifier / 2 - 4;
9292

9393
if (changeLevels){
9494
Game.setChangeLevel(true);

src/com/redomar/game/level/LevelHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ private int getPlayerMPIndex(String username){
175175
public void movePlayer(String username, int x, int y, int numSteps, boolean isMoving, int movingDir){
176176
int index = getPlayerMPIndex(username);
177177
PlayerMP player = (PlayerMP)this.getEntities().get(index);
178-
player.x = x;
179-
player.y = y;
178+
player.setX(x);
179+
player.setY(y);
180180
player.setNumSteps(numSteps);
181181
player.setMoving(isMoving);
182182
player.setMovingDir(movingDir);

src/com/redomar/game/net/GameServer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ private void handleMove(Packet02Move packet) {
8787
if(getPlayerMP(packet.getUsername()) != null){
8888
int index = getPlayerMPIndex(packet.getUsername());
8989
PlayerMP player = this.connectedPlayers.get(index);
90-
player.x = packet.getX();
91-
player.y = packet.getY();
90+
player.setX(packet.getX());
91+
player.setY(packet.getY());
9292
player.setNumSteps(packet.getNumSteps());
9393
player.setMoving(packet.isMoving());
9494
player.setMovingDir(packet.getMovingDir());
@@ -112,7 +112,7 @@ public void addConnection(PlayerMP player, Packet00Login packet) {
112112
} else {
113113
sendData(packet.getData(), p.ipAddess, p.port);
114114

115-
packet = new Packet00Login(p.getUsername(), p.x, p.y);
115+
packet = new Packet00Login(p.getUsername(), p.getX(), p.getY());
116116
sendData(packet.getData(), player.ipAddess, player.port);
117117
}
118118
}

0 commit comments

Comments
 (0)