Lattice Workshop - Step 6.7

Only allow action within the map range

Next step

We only want to allow actions on coords that are within the bounds of the map, so let's add a simple modifier to the spawn and action method.

Files changed (1) hide show
  1. contracts/src/Game.sol +12 -2
contracts/src/Game.sol CHANGED
@@ -54,6 +54,11 @@ contract Game {
54
54
  _;
55
55
  }
56
56
 
57
+ modifier inBounds(Coord memory coord) {
58
+ require(coord.x < width && coord.y < height, 'out of bounds');
59
+ _;
60
+ }
61
+
57
62
  constructor(address _world) {
58
63
  owner = msg.sender;
59
64
  world = _world;
@@ -118,7 +123,7 @@ contract Game {
118
123
  }
119
124
  }
120
125
 
121
- function spawn(Coord memory center) public {
126
+ function spawn(Coord memory center) public inBounds(center) {
122
127
  // Check player is not spawned yet
123
128
  require(c.ownedBy.getEntitiesWithValue(msg.sender).length == 0, 'already spawned');
124
129
 
@@ -161,7 +166,12 @@ contract Game {
161
166
  }
162
167
  }
163
168
 
164
- function action(uint256 entity, Coord memory target) public onlyEntityOwner(entity) onlyAdjacent(entity, target) {
169
+ function action(uint256 entity, Coord memory target)
170
+ public
171
+ onlyEntityOwner(entity)
172
+ onlyAdjacent(entity, target)
173
+ inBounds(target)
174
+ {
165
175
  // Check for mined tiles at the target coord
166
176
  (uint256 targetEntity, bool foundTargetEntity) = getEntityWithAt(c.mined, target);
167
177