Skip to content

Commit 9c09924

Browse files
committed
Merge branch 'npc-dev' into aside
2 parents 839d1dc + 8177ffc commit 9c09924

File tree

4 files changed

+126
-82
lines changed

4 files changed

+126
-82
lines changed

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

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

3+
import java.util.List;
4+
35
import com.redomar.game.Game;
46
import com.redomar.game.entities.efx.Swim;
57
import com.redomar.game.gfx.Colours;
@@ -27,8 +29,13 @@ public Dummy(LevelHandler level, String name, int x, int y, int shirtCol,
2729

2830
public void tick() {
2931

30-
followMovementAI(getX(), getY(), Game.getPlayer().getX(), Game
31-
.getPlayer().getY(), xa, ya, this);
32+
List<Player> players = level.getPlayers(this, 10);
33+
if (players.size() > 0) {
34+
followMovementAI(getX(), getY(), Game.getPlayer().getX(), Game
35+
.getPlayer().getY(), xa, ya, this);
36+
}else{
37+
isMoving = false;
38+
}
3239

3340
setSwim(new Swim(level, getX(), getY()));
3441
swimType = getSwim().swimming(isSwimming, isMagma, isMuddy);
@@ -57,6 +64,9 @@ public void render(Screen screen) {
5764
} else if (movingDir > 1) {
5865
xTile += 4 + ((numSteps >> walkingSpeed) & 1) * 2;
5966
flipTop = (movingDir - 1) % 2;
67+
if(!isMoving){
68+
xTile = 4;
69+
}
6070
}
6171

6272
int modifier = 8 * scale;
@@ -104,39 +114,6 @@ public void render(Screen screen) {
104114
}
105115
}
106116

107-
public boolean hasCollided(int xa, int ya) {
108-
int xMin = 0;
109-
int xMax = 7;
110-
int yMin = 3;
111-
int yMax = 7;
112-
113-
for (int x = xMin; x < xMax; x++) {
114-
if (isSolid(xa, ya, x, yMin)) {
115-
return true;
116-
}
117-
}
118-
119-
for (int x = xMin; x < xMax; x++) {
120-
if (isSolid(xa, ya, x, yMax)) {
121-
return true;
122-
}
123-
}
124-
125-
for (int y = yMin; y < yMax; y++) {
126-
if (isSolid(xa, ya, xMin, y)) {
127-
return true;
128-
}
129-
}
130-
131-
for (int y = yMin; y < yMax; y++) {
132-
if (isSolid(xa, ya, xMax, y)) {
133-
return true;
134-
}
135-
}
136-
137-
return false;
138-
}
139-
140117
public Swim getSwim() {
141118
return swim;
142119
}

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

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,83 @@ public void move(int xa, int ya) {
4343
//2 = Facing Left
4444
//3 = Facing Right
4545

46-
if (!hasCollided(xa, ya)) {
47-
if (ya < 0) {
48-
movingDir = 0;
46+
if (ya < 0) {
47+
movingDir = 0;
48+
}
49+
if (ya > 0) {
50+
movingDir = 1;
51+
}
52+
if (xa < 0) {
53+
movingDir = 2;
54+
}
55+
if (xa > 0) {
56+
movingDir = 3;
57+
}
58+
59+
for (int x = 0; x < Math.abs(xa); x++) {
60+
if (!hasCollided(abs(xa), ya)) {
61+
setX(getX() + abs(xa) * (int) speed);
4962
}
50-
if (ya > 0) {
51-
movingDir = 1;
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);
5268
}
53-
if (xa < 0) {
54-
movingDir = 2;
69+
}
70+
}
71+
72+
public boolean hasCollided(int xa, int ya){
73+
int xMin = 0;
74+
int xMax = 7;
75+
int yMin = 3;
76+
int yMax = 7;
77+
78+
for (int x = xMin; x < xMax; x++) {
79+
if (isSolid(xa, ya, x, yMin)) {
80+
return true;
5581
}
56-
if (xa > 0) {
57-
movingDir = 3;
82+
}
83+
84+
for (int x = xMin; x < xMax; x++) {
85+
if (isSolid(xa, ya, x, yMax)) {
86+
return true;
87+
}
88+
}
89+
90+
for (int y = yMin; y < yMax; y++) {
91+
if (isSolid(xa, ya, xMin, y)) {
92+
return true;
93+
}
94+
}
95+
96+
for (int y = yMin; y < yMax; y++) {
97+
if (isSolid(xa, ya, xMax, y)) {
98+
return true;
5899
}
59-
setX(getX() + xa * (int) speed);
60-
setY(getY() + ya * (int) speed);
61100
}
62-
}
63101

64-
public abstract boolean hasCollided(int xa, int ya);
102+
return false;
103+
}
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+
}
65123

66124
protected boolean isSolid(int xa, int ya, int x, int y) {
67125

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

Lines changed: 11 additions & 34 deletions
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;
@@ -161,39 +163,6 @@ public void render(Screen screen) {
161163
}
162164
}
163165

164-
public boolean hasCollided(int xa, int ya) {
165-
int xMin = 0;
166-
int xMax = 7;
167-
int yMin = 3;
168-
int yMax = 7;
169-
170-
for (int x = xMin; x < xMax; x++) {
171-
if (isSolid(xa, ya, x, yMin)) {
172-
return true;
173-
}
174-
}
175-
176-
for (int x = xMin; x < xMax; x++) {
177-
if (isSolid(xa, ya, x, yMax)) {
178-
return true;
179-
}
180-
}
181-
182-
for (int y = yMin; y < yMax; y++) {
183-
if (isSolid(xa, ya, xMin, y)) {
184-
return true;
185-
}
186-
}
187-
188-
for (int y = yMin; y < yMax; y++) {
189-
if (isSolid(xa, ya, xMax, y)) {
190-
return true;
191-
}
192-
}
193-
194-
return false;
195-
}
196-
197166
public String getUsername() {
198167
if (this.userName.isEmpty()) {
199168
return guestPlayerName;
@@ -221,4 +190,12 @@ public void setSwim(Swim swim) {
221190
this.swim = swim;
222191
}
223192

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

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.redomar.game.Game;
1313
import com.redomar.game.entities.Entity;
14+
import com.redomar.game.entities.Player;
1415
import com.redomar.game.entities.PlayerMP;
1516
import com.redomar.game.gfx.Screen;
1617
import com.redomar.game.level.tiles.Tile;
@@ -192,5 +193,36 @@ public void movePlayer(String username, int x, int y, int numSteps,
192193
player.setMoving(isMoving);
193194
player.setMovingDir(movingDir);
194195
}
196+
197+
public List<Entity> getEntities(Entity e, int radius){
198+
List<Entity> result = new ArrayList<Entity>();
199+
int ex = e.getX();
200+
int ey = e.getY();
201+
for (int i = 0; i < entities.size(); i++) {
202+
Entity entity = entities.get(i);
203+
int x = entity.getX();
204+
int y = entity.getY();
205+
206+
int dx = Math.abs(x - ex);
207+
int dy = Math.abs(y - ey);
208+
209+
double distance = Math.sqrt((dx*2) + (dy*2));
210+
if(distance <= radius){
211+
result.add(entity);
212+
}
213+
}
214+
return result;
215+
}
216+
217+
public List<Player> getPlayers(Entity e, int radius){
218+
List<Entity> entities = getEntities(e, radius);
219+
List<Player> result = new ArrayList<Player>();
220+
for (int i = 0; i < entities.size(); i++) {
221+
if (entities.get(i) instanceof Player) {
222+
result.add((Player) entities.get(i));
223+
}
224+
}
225+
return result;
226+
}
195227

196228
}

0 commit comments

Comments
 (0)