-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.lua
More file actions
176 lines (137 loc) · 4.58 KB
/
game.lua
File metadata and controls
176 lines (137 loc) · 4.58 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
local sceneManager = require "sceneManager"
local game = {}
local characterSprite
local wallTile
local doorTile
local houseWallTile
local keySprite
local characterPosition = {["x"] = 0, ["y"] = 140}
local characterVelocity = {["x"] = 0, ["y"] = 0}
local characterSpeed = 3
local keyPosition = {["x"] = 260, ["y"] = 20}
local doorPosition = {["x"] = 100, ["y"] = 100}
local gotKey = false
local timeForNextBattle = 1
local passedTimeBattle = 0
function game.load()
characterSprite = love.graphics.newImage("assets/character.png")
wallTile = love.graphics.newImage("assets/wall.png")
doorTile = love.graphics.newImage("assets/door.png")
houseWallTile = love.graphics.newImage("assets/house_wall.png")
keySprite = love.graphics.newImage("assets/key.png")
-- sceneManager.changeScene(require "titleScreen")
end
function game.update(dt)
passedTimeBattle = passedTimeBattle + dt
-- if passedTimeBattle > timeForNextBattle then
-- sceneManager.pushScene(require "battle")
-- end
characterVelocity.x = 0
characterVelocity.y = 0
if love.keyboard.isDown("up") then
characterVelocity.y = -characterSpeed
end
if love.keyboard.isDown("down") then
characterVelocity.y = characterSpeed
end
if love.keyboard.isDown("left") then
characterVelocity.x = -characterSpeed
end
if love.keyboard.isDown("right") then
characterVelocity.x = characterSpeed
end
local oldX = characterPosition.x
local oldY = characterPosition.y
characterPosition.x = characterPosition.x + characterVelocity.x
characterPosition.y = characterPosition.y + characterVelocity.y
-- limit character position inside game map
if characterPosition.x <= 20 then
characterPosition.x = 20
elseif characterPosition.x >= 280 then
characterPosition.x = 280
end
if characterPosition.y <= 20 then
characterPosition.y = 20
elseif characterPosition.y >= 140 then
characterPosition.y = 140
end
-- collision with wall tiles
if
characterPosition.x >= 68 - 10 and characterPosition.y >= 68 - 12 and characterPosition.x <= 132 + 12 and
characterPosition.y <= 132 - 20
then
characterPosition.x = oldX
characterPosition.y = oldY
end
-- get key if close enough
if math.abs(characterPosition.x - keyPosition.x) < 10 and math.abs(characterPosition.y - keyPosition.y) < 10 then
gotKey = true
end
end
function game.draw()
love.graphics.push()
love.graphics.scale(4)
-- background
love.graphics.setColor(139 / 255, 90 / 255, 43 / 255, 1.0)
love.graphics.rectangle("fill", 20, 20, 280, 140)
love.graphics.setColor(1, 1, 1) -- reset color
-- map tiles
for x = 20, 300, 16 do
love.graphics.draw(wallTile, x, 4)
end
for x = 20, 300, 16 do
love.graphics.draw(wallTile, x, 160)
end
for y = 20, 160, 16 do
love.graphics.draw(wallTile, 4, y)
end
for y = 20, 160, 16 do
love.graphics.draw(wallTile, 300, y)
end
-- house
love.graphics.draw(houseWallTile, 100 - 16 * 2, 100)
love.graphics.draw(houseWallTile, 100 - 16, 100)
love.graphics.draw(houseWallTile, 100 - 16 * 2, 100 - 16)
love.graphics.draw(houseWallTile, 100 - 16, 100 - 16)
love.graphics.draw(houseWallTile, 100 - 16 * 2, 100 - 32)
love.graphics.draw(houseWallTile, 100 - 16, 100 - 32)
love.graphics.draw(houseWallTile, 100, 100 - 16)
love.graphics.draw(houseWallTile, 100, 100 - 32)
love.graphics.draw(houseWallTile, 100 + 16 * 2, 100)
love.graphics.draw(houseWallTile, 100 + 16, 100)
love.graphics.draw(houseWallTile, 100 + 16 * 2, 100 - 16)
love.graphics.draw(houseWallTile, 100 + 16, 100 - 16)
love.graphics.draw(houseWallTile, 100 + 16 * 2, 100 - 32)
love.graphics.draw(houseWallTile, 100 + 16, 100 - 32)
-- door
love.graphics.draw(doorTile, doorPosition.x, doorPosition.y)
-- character
love.graphics.draw(characterSprite, characterPosition.x, characterPosition.y)
-- key
if not gotKey then
love.graphics.draw(keySprite, keyPosition.x, keyPosition.y)
end
love.graphics.pop()
-- love.graphics.print(characterPosition.x, 0, 0)
-- love.graphics.print(characterPosition.y, 0, 20)
end
function game.keypressed(key)
if key == "space" then
-- open popup
if math.abs(characterPosition.x - doorPosition.x) < 10 and math.abs(characterPosition.y - doorPosition.y) < 15 then
-- try to open door if close enough
if gotKey then
sceneManager.pushScene(require "interact", 2)
else
sceneManager.pushScene(require "interact", 1)
end
else
-- nothing to interact
sceneManager.pushScene(require "interact")
end
end
if key == "escape" then
love.event.quit()
end
end
return game