This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.lua
103 lines (92 loc) · 2.89 KB
/
model.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require("entity")
love.filesystem.load("lib/tiledmap.lua")()
local blocksLayer
local entitiesLayer
FIELD_EMPTY = 0
FIELD_BLOCK = 1
FIELD_LADDER = 2
FIELD_EXITDOORCLOSED = 6
FIELD_EXITDOOR = 7
function loadLevel(num)
TiledMap_Load("map/map" .. tostring(num) .. ".tmx", FIELD_SIZE)
local w = TiledMap_GetMapW()
local h = TiledMap_GetMapH()
entitiesLayer = TiledMap_GetLayerZByName("entities")
blocksLayer = TiledMap_GetLayerZByName("blocks")
entities = {}
game.keys = 0
table.insert(entities, player)
for x = 0,w - 1 do
for y = 0, h - 1 do
local blockTile = TiledMap_GetMapTile(x, y, blocksLayer)
local blockTileProps = TiledMap_GetTileProps(blockTile)
local tile = TiledMap_GetMapTile(x, y, entitiesLayer)
local tileProps = TiledMap_GetTileProps(tile)
if blockTileProps and blockTileProps.type == "key" then
game.keys = game.keys + 1
end
if tileProps and tileProps.name == "player" then
player.x = x + 0.5
player.y = y
player.type = "player"
if tileProps.direction == "left" then
player.dx = -1
else
player.dx = 1
end
elseif tileProps and tileProps.name == "monster" then
local monster = Entity.create("monster")
monster.x = x + 0.5
monster.y = y - 0.01
monster.vx = 0
monster.vy = 0
monster.dy = 0
monster.maxSpeed = 0.05
if tileProps.direction == "left" then
monster.dx = -1
else
monster.dx = 1
end
table.insert(entities, monster)
end
end
end
TiledMap_SetLayerInvisByName("entities")
end
function getMapRange(x1, y1, x2, y2)
local fields = {}
local z = TiledMap_GetLayerZByName("blocks")
if x2 < x1 then
aux = x1
x1 = x2
x2 = aux
end
if y2 < y1 then
aux = y1
y1 = y2
y2 = aux
end
x1 = math.max(0, x1)
y1 = math.max(0, y1)
x2 = math.min(TiledMap_GetMapW(), x2)
y2 = math.min(TiledMap_GetMapH(), y2)
for x=x1,x2 do
for y=y1,y2 do
table.insert(fields, TiledMap_GetMapTile(x, y, blocksLayer))
end
end
return fields
end
function openExitDoors()
local w = TiledMap_GetMapW()
local h = TiledMap_GetMapH()
for x = 0,w - 1 do
for y = 0, h - 1 do
local blockTile = TiledMap_GetMapTile(x, y, blocksLayer)
local blockTileProps = TiledMap_GetTileProps(blockTile)
if blockTileProps and blockTileProps.type == "exit_closed" then
TiledMap_SetMapTile(x, y, blocksLayer, FIELD_EXITDOOR)
end
end
end
end