Skip to content

Commit c594e28

Browse files
committed
TMGE updates:
- RandomMaze regenerates few times so all characters can reach player - Player sloweness - MazeCharacter can have random movement when not following/escaping - Force maze background redraw
1 parent a39a109 commit c594e28

5 files changed

Lines changed: 78 additions & 11 deletions

File tree

src/tge/TGE.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
public class TGE {
2121

22-
public static final String VERSION = "0.19.1";
22+
public static final String VERSION = "0.20.0";
2323

2424
public static void log_version() {
2525
System.out.println("TGE version: " + VERSION);

src/tmge/Maze.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
public class Maze extends SerializableObject {
1212

13+
public static int REGENERATION_ATTEMPTS_ALLOWED = 10;
14+
1315
public int width = 0;
1416
public int height = 0;
1517

@@ -93,15 +95,36 @@ public JSONObject save_object() {
9395
public void cell_init(MazeCell cell) {}
9496

9597
public void reset_cells() {
96-
cells = new MazeCell[this.width][this.height];
97-
for (int i = 0; i < this.width; i++) {
98-
for (int j = 0; j < this.height; j++) {
99-
MazeCell cell = new MazeCell(this, i, j);
100-
cells[i][j] = cell;
101-
cell_init(cell);
98+
for (int attempt = 0; attempt < Maze.REGENERATION_ATTEMPTS_ALLOWED; attempt++) {
99+
cells = new MazeCell[this.width][this.height];
100+
for (int i = 0; i < this.width; i++) {
101+
for (int j = 0; j < this.height; j++) {
102+
MazeCell cell = new MazeCell(this, i, j);
103+
cells[i][j] = cell;
104+
cell_init(cell);
105+
}
102106
}
107+
if (this.maze_ok()) break;
103108
}
104109
maze_layer_dirty = true;
110+
for (MazeCharacter character : this.characters) {
111+
character.path = null;
112+
}
113+
}
114+
115+
protected boolean maze_ok() {
116+
return true;
117+
}
118+
119+
public void refresh_background() {
120+
maze_layer_dirty = true;
121+
}
122+
123+
public void set_background_color(int r, int g, int b) {
124+
this.r = r;
125+
this.g = g;
126+
this.b = b;
127+
this.refresh_background();
105128
}
106129

107130
public void update_maze_layer() {
@@ -175,5 +198,4 @@ public MazeCell pointed_cell() {
175198
}
176199

177200

178-
179201
}

src/tmge/MazeCharacter.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class MazeCharacter extends SerializableObject {
1818
public int max_path = -1;
1919
public int safe_distance = 10;
2020
public int minimum_distance = 1;
21+
public boolean move_randomly = false;
2122

2223
protected MazeCell target_cell;
2324
protected MazeCell escape_cell;
@@ -68,17 +69,27 @@ public JSONObject save_object() {
6869
}
6970

7071
public void set_target(MazeCell target) {
72+
if (this.target_cell == target) return;
7173
this.target_cell = target;
7274
this.escape_cell = null;
7375
this.path = this.path_to(this.target_cell);
7476
}
7577

78+
public void chase_player() {
79+
this.set_target(TMGE.current_cell());
80+
}
81+
7682
public void set_escape_target(MazeCell target) {
83+
if (this.escape_cell == target) return;
7784
this.escape_cell = target;
7885
this.target_cell = null;
7986
this.path = this.escape_path(this.escape_cell);
8087
}
8188

89+
public void escape_player() {
90+
this.set_escape_target(TMGE.current_cell());
91+
}
92+
8293
public ArrayList<Integer> path_to(MazeCell target) {
8394
return path_to(target, this.current_cell(), false);
8495
}
@@ -189,9 +200,13 @@ protected void draw_path() {
189200
TMGE.papplet().popStyle();
190201
}
191202

203+
public boolean ready_to_act() {
204+
return TMGE.papplet().frameCount - slowness > last_acted;
205+
}
206+
192207
public void act() {
193208
// This is to slow the movement of the character
194-
if (TMGE.papplet().frameCount - slowness < last_acted) return;
209+
if (!this.ready_to_act()) return;
195210
if (this.path == null || this.path.isEmpty() || this.maze.maze_layer_dirty) {
196211
if (escape_cell != null) {
197212
this.path = this.escape_path(this.target_cell);
@@ -203,6 +218,9 @@ else if (target_cell != null) {
203218
if (this.path != null && !this.path.isEmpty()) {
204219
this.move_in_direction(this.path.remove(0));
205220
}
221+
else if (this.move_randomly) {
222+
this.move_in_direction((int)(Math.random() * 5));
223+
}
206224
last_acted = TMGE.papplet().frameCount;
207225
}
208226

@@ -223,7 +241,9 @@ public boolean move_in_direction(int direction) {
223241

224242
public boolean move_up() {
225243
if (this.maze == null) return false;
244+
if (!this.ready_to_act()) return false;
226245
if (!current_cell().wall_up()) {
246+
last_acted = TMGE.papplet().frameCount;
227247
y--;
228248
return true;
229249
}
@@ -232,7 +252,9 @@ public boolean move_up() {
232252

233253
public boolean move_down() {
234254
if (maze == null) return false;
255+
if (!this.ready_to_act()) return false;
235256
if (!current_cell().wall_down()) {
257+
last_acted = TMGE.papplet().frameCount;
236258
y++;
237259
return true;
238260
}
@@ -241,7 +263,9 @@ public boolean move_down() {
241263

242264
public boolean move_left() {
243265
if (maze == null) return false;
266+
if (!this.ready_to_act()) return false;
244267
if (!current_cell().wall_left()) {
268+
last_acted = TMGE.papplet().frameCount;
245269
x--;
246270
return true;
247271
}
@@ -250,7 +274,9 @@ public boolean move_left() {
250274

251275
public boolean move_right() {
252276
if (maze == null) return false;
277+
if (!this.ready_to_act()) return false;
253278
if (!current_cell().wall_right()) {
279+
last_acted = TMGE.papplet().frameCount;
254280
x++;
255281
return true;
256282
}

src/tmge/RandomMaze.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ public JSONObject save_object() {
2424
return json;
2525
}
2626

27+
@Override
28+
protected boolean maze_ok() {
29+
for (MazeCharacter character : this.characters) {
30+
if (character.path_to(TMGE.current_cell()) == null) {
31+
return false;
32+
}
33+
}
34+
return true;
35+
}
36+
2737
@Override
2838
public void cell_init(MazeCell cell) {
2939
if (TMGE.papplet().random(1) < wall_factor) {

src/tmge/TMGE.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,19 @@ public static void draw_player() {
113113
TMGE.papplet().pushStyle();
114114
if (player_image == null) {
115115
TMGE.papplet().fill(255);
116-
TMGE.papplet().ellipse((TMGE.player_x + 0.5f)*TMGE.maze_scale, (TMGE.player_y + 0.5f)*TMGE.maze_scale, TMGE.maze_scale*0.8f, TMGE.maze_scale*0.8f);
116+
TMGE.papplet().ellipse(
117+
(TMGE.player_x + 0.5f)*TMGE.maze_scale,
118+
(TMGE.player_y + 0.5f)*TMGE.maze_scale,
119+
TMGE.maze_scale*0.8f,
120+
TMGE.maze_scale*0.8f);
117121
}
118122
else {
119-
TMGE.papplet().image(player_image, TMGE.player_x*TMGE.maze_scale, TMGE.player_y*TMGE.maze_scale, TMGE.maze_scale, TMGE.maze_scale);
123+
TMGE.papplet().image(
124+
player_image,
125+
TMGE.player_x*TMGE.maze_scale + 3,
126+
TMGE.player_y*TMGE.maze_scale + 3,
127+
TMGE.maze_scale - 6,
128+
TMGE.maze_scale - 6);
120129
}
121130
TMGE.papplet().popStyle();
122131
}

0 commit comments

Comments
 (0)