Skip to content

Commit ae730ec

Browse files
Update README.md
1 parent 9923f52 commit ae730ec

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

README.md

+44-33
Original file line numberDiff line numberDiff line change
@@ -295,22 +295,19 @@ I used this state machine library extensively in my [Pac-Man game implementation
295295
This is the state machine controlling the behavior of the ghosts:
296296

297297
```java
298-
beginStateMachine()
299-
300-
.description(this::toString)
298+
ai.beginStateMachine()
299+
.description(name + " AI")
301300
.initialState(LOCKED)
302301

303302
.states()
304303

305304
.state(LOCKED)
306305
.onEntry(() -> {
307-
fnSubsequentState = () -> LOCKED;
308-
setVisible(true);
309-
flashing = false;
306+
visible = true;
307+
recovering = false;
310308
bounty = 0;
311-
placeAt(Tile.at(bed.col(), bed.row()), Tile.SIZE / 2, 0);
312-
setMoveDir(bed.exitDir);
313-
setWishDir(bed.exitDir);
309+
nextState = LOCKED;
310+
placeIntoBed();
314311
})
315312
.onTick(this::move)
316313

@@ -319,30 +316,34 @@ beginStateMachine()
319316
.onExit(() -> forceMoving(Direction.LEFT))
320317

321318
.state(ENTERING_HOUSE)
322-
.onEntry(() -> steering().init()) //TODO should not be necessary
323319
.onTick(this::move)
324320

325321
.state(SCATTERING)
326322
.onTick(() -> {
327-
maybeMeetPacMan(pacMan);
323+
updateMentalHealth();
324+
checkPacManCollision(pacMan);
328325
move();
329326
})
330327

331328
.state(CHASING)
332329
.onTick(() -> {
333-
maybeMeetPacMan(pacMan);
330+
updateMentalHealth();
331+
checkPacManCollision(pacMan);
334332
move();
335333
})
336334

337335
.state(FRIGHTENED)
336+
.timeoutAfter(this::getFrightenedTicks)
338337
.onTick((state, consumed, remaining) -> {
339-
maybeMeetPacMan(pacMan);
338+
updateMentalHealth();
339+
checkPacManCollision(pacMan);
340340
move();
341-
// one flashing animation takes 0.5 sec
342-
flashing = remaining < fnNumFlashes.getAsInt() * 0.5f;
341+
recovering = remaining < getFlashTimeTicks();
343342
})
344343

345344
.state(DEAD)
345+
.timeoutAfter(sec(1))
346+
.onEntry(this::computeBounty)
346347
.onTick((s, consumed, remaining) -> {
347348
if (remaining == 0) {
348349
bounty = 0;
@@ -352,57 +353,67 @@ beginStateMachine()
352353

353354
.transitions()
354355

355-
.when(LOCKED).then(LEAVING_HOUSE)
356-
.on(GhostUnlockedEvent.class)
356+
.when(LOCKED).then(LEAVING_HOUSE).on(GhostUnlockedEvent.class)
357357

358358
.when(LEAVING_HOUSE).then(SCATTERING)
359-
.condition(() -> hasLeftGhostHouse() && fnSubsequentState.get() == SCATTERING)
359+
.condition(() -> justLeftHouse() && nextState == SCATTERING)
360+
.annotation("Outside house")
360361

361362
.when(LEAVING_HOUSE).then(CHASING)
362-
.condition(() -> hasLeftGhostHouse() && fnSubsequentState.get() == CHASING)
363+
.condition(() -> justLeftHouse() && nextState == CHASING)
364+
.annotation("Outside house")
365+
366+
.when(LEAVING_HOUSE).then(FRIGHTENED)
367+
.condition(() -> justLeftHouse() && nextState == FRIGHTENED)
368+
.annotation("Outside house")
363369

364370
.when(ENTERING_HOUSE).then(LEAVING_HOUSE)
365-
.condition(() -> steering().isComplete())
371+
.condition(() -> getSteering().isComplete())
372+
.annotation("Reached bed")
366373

367374
.when(CHASING).then(FRIGHTENED)
368375
.on(PacManGainsPowerEvent.class)
369-
.act(() -> reverseDirection())
376+
.act(this::reverseDirection)
370377

371378
.when(CHASING).then(DEAD)
372379
.on(GhostKilledEvent.class)
373380

374381
.when(CHASING).then(SCATTERING)
375-
.condition(() -> fnSubsequentState.get() == SCATTERING)
376-
.act(() -> reverseDirection())
382+
.condition(() -> nextState == SCATTERING)
383+
.act(this::reverseDirection)
384+
.annotation("Got scattering command")
377385

378386
.when(SCATTERING).then(FRIGHTENED)
379387
.on(PacManGainsPowerEvent.class)
380-
.act(() -> reverseDirection())
388+
.act(this::reverseDirection)
381389

382390
.when(SCATTERING).then(DEAD)
383391
.on(GhostKilledEvent.class)
384392

385393
.when(SCATTERING).then(CHASING)
386-
.condition(() -> fnSubsequentState.get() == CHASING)
387-
.act(() -> reverseDirection())
394+
.condition(() -> nextState == CHASING)
395+
.act(this::reverseDirection)
396+
.annotation("Got chasing command")
388397

389-
.stay(FRIGHTENED)
390-
.on(PacManGainsPowerEvent.class)
391-
.act(() -> restartTimer(FRIGHTENED))
398+
.stay(FRIGHTENED).on(PacManGainsPowerEvent.class)
399+
.act(() -> ai.resetTimer(FRIGHTENED))
392400

393401
.when(FRIGHTENED).then(DEAD)
394402
.on(GhostKilledEvent.class)
395403

396404
.when(FRIGHTENED).then(SCATTERING)
397405
.onTimeout()
398-
.condition(() -> fnSubsequentState.get() == SCATTERING)
406+
.condition(() -> nextState == SCATTERING)
399407

400408
.when(FRIGHTENED).then(CHASING)
401409
.onTimeout()
402-
.condition(() -> fnSubsequentState.get() == CHASING)
410+
.condition(() -> nextState == CHASING)
403411

404412
.when(DEAD).then(ENTERING_HOUSE)
405-
.condition(() -> world.isHouseEntry(tileLocation()))
413+
.condition(this::isAtHouseEntry)
414+
.annotation("Reached house entry")
406415

407-
.endStateMachine();
416+
.endStateMachine();
417+
/*@formatter:on*/
418+
ai.setMissingTransitionBehavior(MissingTransitionBehavior.LOG);
408419
```

0 commit comments

Comments
 (0)