Skip to content

Commit a4eee33

Browse files
committed
Implement "strike" command
1 parent 05574db commit a4eee33

File tree

6 files changed

+53
-16
lines changed

6 files changed

+53
-16
lines changed

controllers/Map.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ function controller.execute(model, args)
3535
elseif command.value == 'right' then
3636
user[1] = user[1] + 1
3737

38+
elseif command.value == 'strike' then
39+
local artifact = model:getArtifact(user[1], user[2] - 1)
40+
if artifact then
41+
model:setTile(user[1], user[2] - 1, artifact)
42+
globals.game.views.Map:update()
43+
end
44+
3845
elseif command.value == 'ifstart' then
3946
local start = model:getPosition('start')
4047
if start[1] == user[1] and start[2] == user[2] then

data/maps/forward.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ local Map = {
2424
{ 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 },
2525
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
2626
},
27+
artifacts = {
28+
default = 3,
29+
},
2730
positions = {
2831
start = { 7, 6 },
2932
exit = { 7, 1 },
@@ -33,4 +36,8 @@ local Map = {
3336
},
3437
}
3538

39+
-- example to add artifacts:
40+
-- Map.artifacts[3] = {}
41+
-- Map.artifacts[3][4] = 1
42+
3643
return Map

game.lua

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function game.event.add(stateName, event, cb)
3434
end
3535

3636
function game.maps.current()
37-
return game.maps[game.maps._current]
37+
return maps[game.maps._current]
3838
end
3939

4040
function game.event.push(event, args)
@@ -45,7 +45,7 @@ end
4545

4646
function game.event.handlers.nextMap()
4747
local current = game.maps._current + 1
48-
if game.maps[current] then
48+
if maps[current] then
4949
game.maps._current = current
5050
else
5151
love.event.quit()
@@ -73,7 +73,7 @@ function game.event.handlers.run()
7373
end
7474

7575
function game.event.handlers.startGame()
76-
log.debug('game.startGame: starting new game')
76+
log.debug('game.startGame: starting new game: ' .. game.maps.current())
7777
game.models.Map:load(game.maps.current())
7878
game.views.Map:update()
7979
game.state:push('Map')
@@ -87,6 +87,7 @@ function game.event.handlers.endGame(args)
8787
game.event.push('startGame')
8888
else
8989
game.models.Map:reset()
90+
game.views.Map:update()
9091
end
9192
end
9293

@@ -109,10 +110,6 @@ function game.load()
109110
game.loadComponent('Menu')
110111
game.loadComponent('Map')
111112

112-
for i=1,#maps do
113-
game.maps[i] = require('data.maps.' .. maps[i])
114-
end
115-
116113
game.models.Menu:addItem('New Game', function()
117114
log.debug('Menu: new game')
118115
game.event.push('resetMap')

models/Map.lua

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ local function table_has(table, value)
1212
return false
1313
end
1414

15-
function model:load(map)
16-
self.map = map
15+
function model:load(mapName)
16+
self.mapName = mapName
17+
self.map = dofile('data/maps/' .. mapName .. '.lua')
1718
self.map.positions.user = {unpack(self.map.positions.start)}
1819
end
1920

2021
function model:reset()
21-
self.map.positions.user = {unpack(self.map.positions.start)}
22+
self:load(self.mapName)
2223
end
2324

2425
function model:getSize()
@@ -34,21 +35,46 @@ function model:getTile(x, y)
3435
end
3536

3637
function model:setTile(x, y, tile)
37-
if self.map.components[tile] then
38+
if self:isTileOnMap(x, y) and self.map.components[tile] then
3839
self.map.data[y][x] = tile
3940
else
4041
error('No component for tile id "' .. tile .. '"')
4142
end
4243
end
4344

44-
function model:isTileMovable(x, y)
45+
function model:isTileOnMap(x, y)
4546
return (x > 0 and y > 0
4647
and x <= self.map.size.width
47-
and y <= self.map.size.height
48+
and y <= self.map.size.height)
49+
end
50+
51+
function model:isTileMovable(x, y)
52+
return (self:isTileOnMap(x, y)
4853
and table_has({'grass', 'exit'},
4954
self:getComponent(self:getTile(x, y))))
5055
end
5156

57+
function model:isTileBreakable(x, y)
58+
return (self:isTileOnMap(x, y)
59+
and table_has({'box_closed'},
60+
self:getComponent(self:getTile(x, y))))
61+
end
62+
63+
function model:hasArtifact(x, y)
64+
return (self.isTileOnMap(x, y) and self.map.artifacts[y][x])
65+
end
66+
67+
function model:getArtifact(x, y)
68+
if self:isTileOnMap(x, y) then
69+
if self.map.artifacts[y] and self.map.artifacts[y][x] then
70+
return self.map.artifacts[y][x]
71+
else
72+
return self.map.artifacts.default
73+
end
74+
end
75+
return nil
76+
end
77+
5278
function model:getComponent(id)
5379
return self.map.components[id]
5480
end

parser.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ local function parse(rawScript)
3030
break
3131
end
3232

33-
if table_has({ 'forward', 'backward', 'left', 'right' }, token) then
33+
if table_has({ 'forward', 'backward', 'left', 'right', 'strike' },
34+
token) then
3435
command = newCommand(token, command)
3536
commands[current] = command
3637
current = current + 1

tests/unit/Map_spec.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
local map = nil
2-
local dummy = require('data.maps.dummy')
32

43
describe('Map', function()
54
setup(function()
65
map = require('models.Map')
76
end)
87

98
before_each(function()
10-
map:load(dummy)
9+
map:load('dummy')
1110
end)
1211

1312
after_each(function()

0 commit comments

Comments
 (0)