Skip to content

Commit 2805019

Browse files
committed
Merged npc-dev with aside
Slowed down the NPC
2 parents 9c09924 + fa95a8d commit 2805019

File tree

8 files changed

+77
-59
lines changed

8 files changed

+77
-59
lines changed

src/com/redomar/game/Game.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void init() {
123123
setMap("/levels/custom_level.png");
124124
setMap(1);
125125
Packet00Login loginPacket = new Packet00Login(player.getUsername(),
126-
player.getX(), player.getY());
126+
(int) player.getX(), (int) player.getY());
127127

128128
if (socketServer != null) {
129129
socketServer.addConnection((PlayerMP) getPlayer(), loginPacket);
@@ -242,8 +242,8 @@ public void render() {
242242
return;
243243
}
244244

245-
int xOffset = getPlayer().getX() - (screen.getWidth() / 2);
246-
int yOffset = getPlayer().getY() - (screen.getHeight() / 2);
245+
int xOffset = (int) getPlayer().getX() - (screen.getWidth() / 2);
246+
int yOffset = (int) getPlayer().getY() - (screen.getHeight() / 2);
247247

248248
getLevel().renderTiles(screen, xOffset, yOffset);
249249

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,33 @@ public class Dummy extends Mob {
1212

1313
private int colour, shirtCol, faceCol; // = Colours.get(-1, 111, 240, 310);
1414
private int tickCount = 0;
15-
private int xa = 0;
16-
private int ya = 0;
15+
private double xa = 0;
16+
private double ya = 0;
1717
private boolean[] swimType;
1818
private int[] swimColour;
19+
private static double speed = 0.75;
1920

2021
private Swim swim;
2122

2223
public Dummy(LevelHandler level, String name, int x, int y, int shirtCol,
2324
int faceCol) {
24-
super(level, "h", x, y, 1);
25+
super(level, "h", x, y, speed);
2526
this.faceCol = faceCol;
2627
this.shirtCol = shirtCol;
2728
this.colour = Colours.get(-1, 111, shirtCol, faceCol);
2829
}
2930

3031
public void tick() {
3132

32-
List<Player> players = level.getPlayers(this, 10);
33+
List<Player> players = level.getPlayers(this, 8);
3334
if (players.size() > 0) {
34-
followMovementAI(getX(), getY(), Game.getPlayer().getX(), Game
35-
.getPlayer().getY(), xa, ya, this);
35+
followMovementAI((int) getX(), (int) getY(), (int) Game.getPlayer().getX(), (int) Game
36+
.getPlayer().getY(), xa, ya, speed, this);
3637
}else{
3738
isMoving = false;
3839
}
3940

40-
setSwim(new Swim(level, getX(), getY()));
41+
setSwim(new Swim(level, (int) getX(), (int) getY()));
4142
swimType = getSwim().swimming(isSwimming, isMagma, isMuddy);
4243
isSwimming = swimType[0];
4344
isMagma = swimType[1];
@@ -70,8 +71,8 @@ public void render(Screen screen) {
7071
}
7172

7273
int modifier = 8 * scale;
73-
int xOffset = getX() - modifier / 2;
74-
int yOffset = getY() - modifier / 2 - 4;
74+
int xOffset = (int) getX() - modifier / 2;
75+
int yOffset = (int) getY() - modifier / 2 - 4;
7576

7677
if (isSwimming || isMagma || isMuddy) {
7778
swimColour = getSwim().waveCols(isSwimming, isMagma, isMuddy);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public abstract class Entity {
77

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

1111
public Entity(LevelHandler level) {
@@ -20,15 +20,15 @@ public final void init(LevelHandler level) {
2020

2121
public abstract void render(Screen screen);
2222

23-
public int getX() {
23+
public double getX() {
2424
return x;
2525
}
2626

2727
public void setX(int x) {
2828
this.x = x;
2929
}
3030

31-
public int getY() {
31+
public double getY() {
3232
return y;
3333
}
3434

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

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public Mob(LevelHandler level, String name, int x, int y, double speed) {
2828
this.speed = speed;
2929
}
3030

31-
public void move(int xa, int ya) {
31+
public void move(double xa, double ya) {
3232
if (xa != 0 && ya != 0) {
3333
move(xa, 0);
3434
move(0, ya);
@@ -55,46 +55,63 @@ public void move(int xa, int ya) {
5555
if (xa > 0) {
5656
movingDir = 3;
5757
}
58-
59-
for (int x = 0; x < Math.abs(xa); x++) {
60-
if (!hasCollided(abs(xa), ya)) {
61-
setX(getX() + abs(xa) * (int) speed);
58+
59+
while (xa != 0){
60+
if (Math.abs(xa) > 1){
61+
if (!hasCollided(abs(xa), ya)) {
62+
this.x += abs(xa);
63+
}
64+
xa -= abs(xa);
65+
} else {
66+
if (!hasCollided(abs(xa), ya)) {
67+
this.x += xa;
68+
}
69+
xa = 0;
6270
}
6371
}
6472

65-
for (int y = 0; y < Math.abs(ya); y++) {
66-
if (!hasCollided(xa, abs(ya))) {
67-
setY(getY() + abs(ya) * (int) speed);
73+
while (ya != 0){
74+
if (Math.abs(ya) > 1){
75+
if (!hasCollided(xa, abs(ya))) {
76+
this.y += abs(ya);
77+
}
78+
ya -= abs(ya);
79+
} else {
80+
if (!hasCollided(xa, abs(ya))) {
81+
this.y += ya;
82+
}
83+
ya = 0;
6884
}
6985
}
86+
7087
}
7188

72-
public boolean hasCollided(int xa, int ya){
89+
public boolean hasCollided(double xa, double ya){
7390
int xMin = 0;
7491
int xMax = 7;
7592
int yMin = 3;
7693
int yMax = 7;
7794

7895
for (int x = xMin; x < xMax; x++) {
79-
if (isSolid(xa, ya, x, yMin)) {
96+
if (isSolid((int) xa, (int) ya, x, yMin)) {
8097
return true;
8198
}
8299
}
83100

84101
for (int x = xMin; x < xMax; x++) {
85-
if (isSolid(xa, ya, x, yMax)) {
102+
if (isSolid((int) xa, (int) ya, x, yMax)) {
86103
return true;
87104
}
88105
}
89106

90107
for (int y = yMin; y < yMax; y++) {
91-
if (isSolid(xa, ya, xMin, y)) {
108+
if (isSolid((int) xa, (int) ya, xMin, y)) {
92109
return true;
93110
}
94111
}
95112

96113
for (int y = yMin; y < yMax; y++) {
97-
if (isSolid(xa, ya, xMax, y)) {
114+
if (isSolid((int) xa, (int) ya, xMax, y)) {
98115
return true;
99116
}
100117
}
@@ -105,8 +122,8 @@ public boolean hasCollided(int xa, int ya){
105122
public boolean hasCollidedAlt(int xa, int ya){
106123
boolean solid = false;
107124
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;
125+
double xt = ((x + xa) - c % 2 * 8) / 8;
126+
double yt = ((y + ya) - c / 2 * 8) / 8;
110127
int ix = (int) Math.ceil(xt);
111128
int iy = (int) Math.ceil(yt);
112129
if (c % 2 == 0) ix = (int) Math.floor(xt);
@@ -116,7 +133,7 @@ public boolean hasCollidedAlt(int xa, int ya){
116133
return solid;
117134
}
118135

119-
private int abs(int i){
136+
private int abs(double i){
120137
if (i < 0) return -1;
121138
return 1;
122139
}
@@ -127,9 +144,9 @@ protected boolean isSolid(int xa, int ya, int x, int y) {
127144
return false;
128145
}
129146

130-
Tile lastTile = level.getTile((this.getX() + x) >> 3,
131-
(this.getY() + y) >> 3);
132-
Tile newtTile = level.getTile((this.getX() + x + xa) >> 3, (this.getY()
147+
Tile lastTile = level.getTile(((int) this.getX() + x) >> 3,
148+
((int) this.getY() + y) >> 3);
149+
Tile newtTile = level.getTile(((int) this.getX() + x + xa) >> 3, ((int) this.getY()
133150
+ y + ya) >> 3);
134151

135152
if (!lastTile.equals(newtTile) && newtTile.isSolid()) {
@@ -139,22 +156,22 @@ protected boolean isSolid(int xa, int ya, int x, int y) {
139156
return false;
140157
}
141158

142-
protected void followMovementAI(int x, int y, int px, int py, int xa,
143-
int ya, Mob mob) {
159+
protected void followMovementAI(int x, int y, int px, int py, double xa,
160+
double ya, double speed, Mob mob) {
144161
ya = 0;
145162
xa = 0;
146163
if (px > x)
147-
xa++;
164+
xa+=speed;
148165
if (px < x)
149-
xa--;
166+
xa-=speed;
150167
if (py > y)
151-
ya++;
168+
ya+=speed;
152169
if (py < y)
153-
ya--;
170+
ya-=speed;
154171
moveMob(xa, ya, mob);
155172
}
156173

157-
protected void moveMob(int xa, int ya, Mob mob) {
174+
protected void moveMob(double xa, double ya, Mob mob) {
158175
if (xa != 0 || ya != 0) {
159176
mob.move(xa, ya);
160177
mob.isMoving = true;

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ public Player(LevelHandler level, int x, int y, InputHandler input,
3838
}
3939

4040
public void tick() {
41-
int xa = 0;
42-
int ya = 0;
41+
double xa = 0;
42+
double ya = 0;
4343

4444
if (input != null) {
4545
if (input.getUp().isPressed()) {
46-
ya--;
46+
ya -= speed;
4747
}
4848
if (input.getDown().isPressed()) {
49-
ya++;
49+
ya += speed;
5050
}
5151
if (input.getLeft().isPressed()) {
52-
xa--;
52+
xa -= speed;
5353
}
5454
if (input.getRight().isPressed()) {
55-
xa++;
55+
xa += speed;
5656
}
5757
}
5858

@@ -61,7 +61,7 @@ public void tick() {
6161
isMoving = true;
6262

6363
Packet02Move packet = new Packet02Move(this.getUsername(),
64-
this.getX(), this.getY(), this.numSteps, this.isMoving,
64+
(int) this.getX(), (int) this.getY(), this.numSteps, this.isMoving,
6565
this.movingDir);
6666
Game.getGame();
6767
packet.writeData(Game.getSocketClient());
@@ -70,13 +70,13 @@ public void tick() {
7070
isMoving = false;
7171
}
7272

73-
setSwim(new Swim(level, getX(), getY()));
73+
setSwim(new Swim(level, (int) getX(), (int) getY()));
7474
swimType = getSwim().swimming(isSwimming, isMagma, isMuddy);
7575
isSwimming = swimType[0];
7676
isMagma = swimType[1];
7777
isMuddy = swimType[2];
7878

79-
if (level.getTile(this.getX() >> 3, this.getY() >> 3).getId() == 11) {
79+
if (level.getTile((int) this.getX() >> 3, (int) this.getY() >> 3).getId() == 11) {
8080
changeLevels = true;
8181
}
8282

@@ -108,8 +108,8 @@ public void render(Screen screen) {
108108
}
109109

110110
int modifier = 8 * scale;
111-
int xOffset = getX() - modifier / 2;
112-
int yOffset = getY() - modifier / 2 - 4;
111+
int xOffset = (int) getX() - modifier / 2;
112+
int yOffset = (int) getY() - modifier / 2 - 4;
113113

114114
if (changeLevels) {
115115
Game.setChangeLevel(true);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,12 @@ public void movePlayer(String username, int x, int y, int numSteps,
196196

197197
public List<Entity> getEntities(Entity e, int radius){
198198
List<Entity> result = new ArrayList<Entity>();
199-
int ex = e.getX();
200-
int ey = e.getY();
199+
int ex = (int) e.getX();
200+
int ey = (int) e.getY();
201201
for (int i = 0; i < entities.size(); i++) {
202202
Entity entity = entities.get(i);
203-
int x = entity.getX();
204-
int y = entity.getY();
203+
int x = (int) entity.getX();
204+
int y = (int) entity.getY();
205205

206206
int dx = Math.abs(x - ex);
207207
int dy = Math.abs(y - ey);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void addConnection(PlayerMP player, Packet00Login packet) {
114114
} else {
115115
sendData(packet.getData(), p.ipAddess, p.port);
116116

117-
packet = new Packet00Login(p.getUsername(), p.getX(), p.getY());
117+
packet = new Packet00Login(p.getUsername(), (int) p.getX(), (int) p.getY());
118118
sendData(packet.getData(), player.ipAddess, player.port);
119119
}
120120
}

src/com/redomar/game/objects/Inventory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class Inventory {
1212
private static InventoryWindow inv_window = new InventoryWindow();
1313

1414
public static void activate() {
15-
x = Game.getPlayer().getX();
16-
y = Game.getPlayer().getY();
15+
x = (int) Game.getPlayer().getX();
16+
y = (int) Game.getPlayer().getY();
1717

1818
if (Game.getLevel().getTile(x >> 3, y >> 3).getId() == 8) {
1919
if (!reset) {

0 commit comments

Comments
 (0)