Skip to content

Commit fe2ec25

Browse files
committed
Refactor: flatten actions
Flatten the action hierarchy, removing sub-actions. Update the UI and logic to reflect these changes.
1 parent a33cc15 commit fe2ec25

File tree

4 files changed

+36
-115
lines changed

4 files changed

+36
-115
lines changed

escape_wild/assets/vanilla/l10n/en.yaml

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,22 @@ attribute:
55
energy: Energy
66
action:
77
# Move forward
8-
move:
9-
name: Move # no direction specified
10-
left: Left
11-
right: Right
12-
up: Up
13-
down: Down
14-
forward: Forward
15-
backward: Backward
8+
move: Move
169
# Explore the surroundings
17-
explore:
18-
name: Explore
19-
gather:
20-
name: Gather
21-
get-water: Get Water
22-
get-wood: Get Wood
23-
get-food: Get Food
24-
hunt:
25-
name: Hunt
26-
trap: With Trap
27-
gun: With Gun
10+
explore: Explore
11+
gather: Gather
12+
get-water: Get Water
13+
get-wood: Get Wood
14+
get-food: Get Food
15+
hunt: Hunt
2816
# Go fishing
29-
fish:
30-
name: Fish
17+
fish: Fish
18+
shelter: Shelter
19+
campfire: Campfire
3120
# When player win the game
32-
escape-wild:
33-
name: Escape Wild
21+
escape-wild: Escape Wild
3422
# When player lose the game
35-
stop-heartbeat:
36-
name: Stop Heartbeat
37-
shelter:
38-
name: Shelter
39-
sleep-till-tomorrow: Sleep Till Tomorrow
40-
# Reinforce the shelter
41-
reinforce: Reinforce
42-
# Have a rest
43-
rest: Rest
23+
stop-heartbeat: Stop Heartbeat
4424
use-type:
4525
use: Use
4626
eat: Eat
Lines changed: 12 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,37 @@
11
import 'package:escape_wild/core/index.dart';
22

3-
/// Full name: User Action.
4-
///
53
/// It's to disambiguate with `Action` in flutter.
64
class UserAction with Moddable {
7-
final UserAction? parent;
85
@override
96
final String name;
10-
final List<UserAction> subActions = [];
117

12-
UserAction(this.name, {this.parent}) {
13-
parent?.subActions.add(this);
14-
}
8+
UserAction(this.name);
159

16-
/// The sub-action should be in `parent-name.sub-name`.
17-
UserAction sub(String name) => UserAction("${this.name}.$name", parent: this);
10+
String l10nName() => i18n("action.$name");
1811

1912
factory UserAction.named(String name) => UserAction(name);
2013

21-
String l10nName() {
22-
final parent = this.parent;
23-
if (parent != null) {
24-
return i18n("action.$name");
25-
} else {
26-
// It's at the top level.
27-
return i18n("action.$name.name");
28-
}
29-
}
30-
31-
bool belongsToOrSelf(UserAction ancestorOrSelf) => this == ancestorOrSelf || belongsTo(ancestorOrSelf);
32-
33-
bool belongsTo(UserAction ancestor) {
34-
var cur = parent;
35-
while (cur != null) {
36-
if (cur == ancestor) return true;
37-
cur = parent?.parent;
38-
}
39-
return false;
40-
}
41-
4214
@override
4315
String toString() => name;
4416

4517
// Move
4618
static final //
4719
move = UserAction("move"),
48-
moveLeft = move.sub("left"),
49-
moveRight = move.sub("right"),
50-
moveUp = move.sub("up"),
51-
moveDown = move.sub("down"),
52-
moveForward = move.sub("forward"),
53-
moveBackward = move.sub("backward");
54-
static final //
55-
explore = UserAction("explore");
56-
static final //
20+
explore = UserAction("explore"),
5721
gather = UserAction("gather"),
58-
gatherGetWater = gather.sub("get-water"),
59-
gatherGetWood = gather.sub("get-wood"),
60-
gatherGetFood = gather.sub("get-food");
61-
62-
static final //
22+
getWood = UserAction("get-wood"),
23+
getWater = UserAction("get-water"),
24+
getFood = UserAction("get-food"),
6325
shelter = UserAction("shelter"),
64-
shelterSleepTillTomorrow = shelter.sub("sleep-till-tomorrow"),
65-
shelterReinforce = shelter.sub("reinforce"),
66-
shelterRest = shelter.sub("rest");
67-
68-
static final //
69-
campfire = UserAction("campfire");
70-
static final //
26+
campfire = UserAction("campfire"),
7127
hunt = UserAction("hunt"),
72-
hunTrap = hunt.sub("trap"),
73-
hunGun = hunt.sub("gun");
74-
static final //
7528
fish = UserAction("fish");
7629

77-
// Win or lose the game.
78-
static final escapeWild = UserAction("escape-wild"), stopHeartbeat = UserAction("stop-heartbeat");
30+
static final
31+
// Win the game
32+
escapeWild = UserAction("escape-wild"),
33+
// Lose the game
34+
stopHeartbeat = UserAction("stop-heartbeat");
7935

8036
static final List<UserAction> defaultActions = [
8137
move,
@@ -93,9 +49,3 @@ class UserAction with Moddable {
9349
@override
9450
int get hashCode => name.hashCode;
9551
}
96-
97-
extension UActionX on UserAction {
98-
bool get isTopLevel => parent == null;
99-
100-
bool get isLeaf => subActions.isEmpty;
101-
}

escape_wild/lib/core/route/route.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,17 @@ extension PlaceProps on PlaceProtocol {
7878
mixin PlaceActionDelegateMixin on PlaceProtocol {
7979
@override
8080
Future<void> performAction(UserAction action) async {
81-
if (action.belongsToOrSelf(UserAction.explore)) {
81+
if (action == UserAction.explore) {
8282
await performExplore();
83-
} else if (action.belongsToOrSelf(UserAction.move)) {
83+
} else if (action == UserAction.move) {
8484
await performMove(action);
85-
} else if (action.belongsToOrSelf(UserAction.gather)) {
85+
} else if (action == UserAction.gather) {
8686
await performGather(action);
87-
} else if (action.belongsToOrSelf(UserAction.fish)) {
87+
} else if (action == UserAction.fish) {
8888
await performFish();
89-
} else if (action.belongsToOrSelf(UserAction.shelter)) {
89+
} else if (action == UserAction.shelter) {
9090
await performShelter(action);
91-
} else if (action.belongsToOrSelf(UserAction.hunt)) {
91+
} else if (action == UserAction.hunt) {
9292
await performHunt(action);
9393
} else {
9494
await performOthers(action);

escape_wild/lib/game/routes/subtropics.dart

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ final fishWithTool = PlaceAction(
3535
),
3636
);
3737
final cutDownTreeWithTool = PlaceAction(
38-
UserAction.gatherGetWood,
38+
UserAction.getWood,
3939
() =>
4040
player.energy > 0.0 &&
4141
player.backpack.hasAnyToolOfType(
4242
ToolType.axe,
4343
),
4444
);
4545

46-
final rest = PlaceAction(UserAction.shelterRest, () => true);
4746
final shelter = PlaceAction(UserAction.shelter, () => true);
4847
final stopHeartbeatAndLose = PlaceAction(UserAction.stopHeartbeat, () => true);
4948
final escapeWildAndWin = PlaceAction(UserAction.escapeWild, () => true);
@@ -230,9 +229,7 @@ class SubtropicsRoute extends RouteProtocol with IterableMixin<SubtropicsPlace>
230229
@override
231230
PlaceProtocol get initialPlace => places[0];
232231

233-
bool get canMoveForward => routeProgress.toInt() < placeCount;
234-
235-
bool get canMoveBackward => 0 < routeProgress.toInt();
232+
bool get canMove => routeProgress.toInt() < placeCount;
236233

237234
@override
238235
Future<void> onPassTime(Ts delta) async {
@@ -306,8 +303,7 @@ class SubtropicsPlace extends CampfirePlaceProtocol with PlaceActionDelegateMixi
306303

307304
SubtropicsPlace(this.name);
308305

309-
late final PlaceAction forward = PlaceAction(UserAction.moveForward, () => route.canMoveForward);
310-
late final PlaceAction backward = PlaceAction(UserAction.moveBackward, () => route.canMoveBackward);
306+
late final PlaceAction forward = PlaceAction(UserAction.move, () => route.canMove);
311307

312308
/// Short name to reduce json size.
313309
@JsonKey(name: "ec")
@@ -331,7 +327,6 @@ class SubtropicsPlace extends CampfirePlaceProtocol with PlaceActionDelegateMixi
331327
@override
332328
List<PlaceAction> getAvailableActions() {
333329
return [
334-
backward,
335330
forward,
336331
exploreWithEnergy,
337332
shelter,
@@ -349,11 +344,7 @@ class SubtropicsPlace extends CampfirePlaceProtocol with PlaceActionDelegateMixi
349344
player.modifyX(Attr.water, -0.0001 * f);
350345
player.modifyX(Attr.energy, -0.0001 * f);
351346
var routeProgress = route.getRouteProgress();
352-
if (action == UserAction.moveForward) {
353-
await route.setRouteProgress(routeProgress + 0.03 * f);
354-
} else if (action == UserAction.moveBackward) {
355-
await route.setRouteProgress(routeProgress - 0.03 * f);
356-
}
347+
await route.setRouteProgress(routeProgress + 0.03 * f);
357348
player.journeyProgress = route.journeyProgress;
358349
player.location = route.current;
359350
});
@@ -498,9 +489,9 @@ class ForestPlace extends SubtropicsPlace {
498489
var isToolBroken = false;
499490
isToolBroken = player.damageTool(tool.stack, comp, dmg * 30.0);
500491
if (isToolBroken) {
501-
await showToolBroken(UserAction.gatherGetWood, tool.stack);
492+
await showToolBroken(UserAction.getWood, tool.stack);
502493
}
503-
await showGain(UserAction.gatherGetWood, gain);
494+
await showGain(UserAction.getWood, gain);
504495
}
505496

506497
@override

0 commit comments

Comments
 (0)