Skip to content

Commit 004129b

Browse files
committed
Added new NPC Vendor
Added a new NPC who is a vendor or trader Added a new random movement AI for vendor Changed the spawn position of dummy closer to the player spawn
1 parent de61c81 commit 004129b

File tree

3 files changed

+182
-1
lines changed

3 files changed

+182
-1
lines changed

src/com/redomar/game/Game.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.redomar.game.entities.Dummy;
1818
import com.redomar.game.entities.Player;
1919
import com.redomar.game.entities.PlayerMP;
20+
import com.redomar.game.entities.Vendor;
2021
import com.redomar.game.gfx.Screen;
2122
import com.redomar.game.gfx.SpriteSheet;
2223
import com.redomar.game.level.LevelHandler;
@@ -76,6 +77,7 @@ public class Game extends Canvas implements Runnable {
7677
private LevelHandler level;
7778
private Player player;
7879
private Dummy dummy;
80+
private Vendor vendor;
7981
private Music music = new Music();
8082
private Font font = new Font();
8183
private Thread musicThread = new Thread(music, "MUSIC");
@@ -138,6 +140,9 @@ public void init() {
138140

139141
// socketClient.sendData("ping".getBytes());
140142
loginPacket.writeData(getSocketClient());
143+
144+
game.setVendor(new Vendor(getLevel(), "g", 215, 215, 304, 543));
145+
getLevel().addEntity(getVendor());
141146
}
142147

143148
public void setMap(String Map_str) {
@@ -161,7 +166,7 @@ public void setMap(String Map_str) {
161166

162167
public static void npcSpawn() {
163168
if (isNpc() == true) {
164-
game.setDummy(new Dummy(Game.getLevel(), "h", 215, 215, 500, 543));
169+
game.setDummy(new Dummy(Game.getLevel(), "h", 100, 150, 500, 543));
165170
game.level.addEntity(Game.getDummy());
166171
}
167172
}
@@ -298,9 +303,11 @@ public void render() {
298303
if (getMap() == 1) {
299304
setMap("/levels/water_level.png");
300305
setMap(2);
306+
getLevel().removeEntity(getVendor());
301307
} else if (getMap() == 2) {
302308
setMap("/levels/custom_level.png");
303309
setMap(1);
310+
getLevel().addEntity(getVendor());
304311
}
305312
changeLevel = false;
306313
}
@@ -495,6 +502,14 @@ public void setDummy(Dummy dummy) {
495502
this.dummy = dummy;
496503
}
497504

505+
public Vendor getVendor() {
506+
return vendor;
507+
}
508+
509+
public void setVendor(Vendor vendor) {
510+
this.vendor = vendor;
511+
}
512+
498513
public static String getJdata_IP() {
499514
return Jdata_IP;
500515
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,25 @@ protected void followMovementAI(int x, int y, int px, int py, double xa,
205205
ya-=speed;
206206
moveMob(xa, ya, mob);
207207
}
208+
209+
protected double[] randomMovementAI(double x, double y, double xa, double ya, int tick) {
210+
if (tick % (random.nextInt(50) + 30) == 0) {
211+
xa = random.nextInt(3) - 1;
212+
ya = random.nextInt(3) - 1;
213+
if (random.nextInt(4) == 0) {
214+
xa = 0;
215+
ya = 0;
216+
}
217+
}
218+
if(x <= 180){
219+
xa = 1;
220+
ya = -1;
221+
}
222+
double move[] = new double[2];
223+
move[0] = xa;
224+
move[1] = ya;
225+
return move;
226+
}
208227

209228
protected void moveMob(double xa, double ya, Mob mob) {
210229
if (xa != 0 || ya != 0) {
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package com.redomar.game.entities;
2+
3+
import java.util.List;
4+
5+
import com.redomar.game.entities.efx.Swim;
6+
import com.redomar.game.gfx.Colours;
7+
import com.redomar.game.gfx.Screen;
8+
import com.redomar.game.level.LevelHandler;
9+
import com.redomar.game.level.Node;
10+
11+
public class Vendor extends Mob {
12+
13+
private int colour, shirtCol, faceCol; // = Colours.get(-1, 111, 240, 310);
14+
private int tickCount = 0;
15+
private int tick = 0;
16+
private double xa = 0;
17+
private double ya = 0;
18+
private double[] movement;
19+
private boolean[] swimType;
20+
private int[] swimColour;
21+
private static double speed = 0.75;
22+
private List<Node> path = null;
23+
private int time = 0;
24+
private static int[] collisionBoders = {0, 7, 0, 7};
25+
26+
private Swim swim;
27+
28+
public Vendor(LevelHandler level, String name, int x, int y, int shirtCol,
29+
int faceCol) {
30+
super(level, "h", x, y, speed, collisionBoders);
31+
this.faceCol = faceCol;
32+
this.shirtCol = shirtCol;
33+
this.colour = Colours.get(-1, 111, shirtCol, faceCol);
34+
}
35+
36+
public void tick() {
37+
38+
tick++;
39+
movement = randomMovementAI(x, y, xa, ya, tick);
40+
41+
this.xa = movement[0];
42+
this.ya = movement[1];
43+
44+
moveMob(xa, ya, this);
45+
46+
setSwim(new Swim(level, (int) getX(), (int) getY()));
47+
swimType = getSwim().swimming(isSwimming, isMagma, isMuddy);
48+
isSwimming = swimType[0];
49+
isMagma = swimType[1];
50+
isMuddy = swimType[2];
51+
52+
tickCount++;
53+
54+
}
55+
56+
public void render(Screen screen) {
57+
setTime(getTime() + 1);
58+
int xTile = 8;
59+
int yTile = 28;
60+
int walkingSpeed = 4;
61+
int flipTop = (numSteps >> walkingSpeed) & 1;
62+
int flipBottom = (numSteps >> walkingSpeed) & 1;
63+
64+
if (movingDir == 1) {
65+
xTile += 2;
66+
if (!isMoving || swim.isActive(swimType)){
67+
yTile -= 2;
68+
}
69+
} else if (movingDir == 0 && !isMoving || movingDir == 0 && swim.isActive(swimType)) {
70+
yTile -= 2;
71+
} else if (movingDir > 1) {
72+
xTile += 4 + ((numSteps >> walkingSpeed) & 1) * 2;
73+
flipTop = (movingDir - 1) % 2;
74+
if(!isMoving){
75+
xTile = 4;
76+
}
77+
}
78+
79+
int modifier = 8 * scale;
80+
int xOffset = (int) getX() - modifier / 2;
81+
int yOffset = (int) getY() - modifier / 2 - 4;
82+
83+
if (isSwimming || isMagma || isMuddy) {
84+
swimColour = getSwim().waveCols(isSwimming, isMagma, isMuddy);
85+
86+
int waterColour = 0;
87+
yOffset += 4;
88+
89+
colour = Colours.get(-1, 111, -1, faceCol);
90+
91+
if (tickCount % 60 < 15) {
92+
waterColour = Colours.get(-1, -1, swimColour[0], -1);
93+
} else if (15 <= tickCount % 60 && tickCount % 60 < 30) {
94+
yOffset--;
95+
waterColour = Colours.get(-1, swimColour[1], swimColour[2], -1);
96+
} else if (30 <= tickCount % 60 && tickCount % 60 < 45) {
97+
waterColour = Colours.get(-1, swimColour[2], -1, swimColour[1]);
98+
} else {
99+
yOffset--;
100+
waterColour = Colours.get(-1, -1, swimColour[1], swimColour[2]);
101+
}
102+
103+
screen.render(xOffset, yOffset + 3, 31 + 31 * 32, waterColour,
104+
0x00, 1);
105+
screen.render(xOffset + 8, yOffset + 3, 31 + 31 * 32, waterColour,
106+
0x01, 1);
107+
}
108+
109+
screen.render((xOffset + (modifier * flipTop)), yOffset,
110+
(xTile + yTile * 32), colour, flipTop, scale);
111+
screen.render((xOffset + modifier - (modifier * flipTop)), yOffset,
112+
((xTile + 1) + yTile * 32), colour, flipTop, scale);
113+
if (!isSwimming && !isMagma && !isMuddy) {
114+
screen.render((xOffset + (modifier * flipBottom)),
115+
(yOffset + modifier), (xTile + (yTile + 1) * 32), colour,
116+
flipBottom, scale);
117+
screen.render((xOffset + modifier - (modifier * flipBottom)),
118+
(yOffset + modifier), ((xTile + 1) + (yTile + 1) * 32),
119+
colour, flipBottom, scale);
120+
colour = Colours.get(-1, 111, shirtCol, faceCol);
121+
}
122+
}
123+
124+
public Swim getSwim() {
125+
return swim;
126+
}
127+
128+
public void setSwim(Swim swim) {
129+
this.swim = swim;
130+
}
131+
132+
public List<Node> getPath() {
133+
return path;
134+
}
135+
136+
public void setPath(List<Node> path) {
137+
this.path = path;
138+
}
139+
140+
public int getTime() {
141+
return time;
142+
}
143+
144+
public void setTime(int time) {
145+
this.time = time;
146+
}
147+
}

0 commit comments

Comments
 (0)