Skip to content

Commit fa95a8d

Browse files
committed
Slowed down the speed of Dummy(NPC) and Reduced the area in which the NPC follows the player
1 parent d645fba commit fa95a8d

File tree

3 files changed

+50
-32
lines changed

3 files changed

+50
-32
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,28 @@ 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) {
3435
followMovementAI((int) getX(), (int) getY(), (int) Game.getPlayer().getX(), (int) Game
35-
.getPlayer().getY(), xa, ya, this);
36+
.getPlayer().getY(), xa, ya, speed, this);
3637
}else{
3738
isMoving = false;
3839
}

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

Lines changed: 38 additions & 21 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-
this.x += 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-
this.y += 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
}
@@ -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
}
@@ -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: 6 additions & 6 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

0 commit comments

Comments
 (0)