-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_control.lua
244 lines (233 loc) · 9.35 KB
/
game_control.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
function handle_collision(id, other, cols)
if game.players[id] then
if game.coins[other] then
kill_entity(other)
end
end
end
function spawn_from_map(map,layername,hide_layer)
local layer = map.layers[layername]
if layer then
for x = 1, layer.width do
for y = 1, layer.height do
local tile = layer.data[x+(y-1)*layer.width] - 1
if tile ~= nil then
local tileset = nil
for _,ts in pairs(map.tilesets) do
if(tile < ts.lastgid) then
tileset = ts
break
end
end
if tile > -1 then
local properties = tileset.properties[tile]
if properties then
for key,val in pairs(properties) do
if key == "interactable_type" then
add_interactable(x,y,val)
end
end
end
end
end
end
end
if hide_layer then
layer.visible = false
end
else
print("Error in spawn_from_map: layer\""..layername.."\" does not exist")
end
end
function interact(player_id)
local use_range= math.pow(18,2)
local interacted = false
if game.players[player_id].available_interactable then
local id = game.players[player_id].available_interactable
local interactable = game.interactables[id]
if not interacted then
interactable.functions.interact(id,player_id)
end
interacted = true
end
if not interacted then
local inv = game.players[player_id].inventory[game.players[player_id].active_inventory_slot]
if inv ~= nil then
local interactable = game.interactables[inv]
if interactable ~= nil and interactable.functions ~= nil and interactable.functions.use ~= nil then
interactable.functions.use(player_id)
end
end
end
end
function update_player(dt,id)
local accel = 600.0
local walking = false
local player_num = 1
if game.player_ids[2] == id then player_num=2 end
if game.players[id].gui == nil then
if love.keyboard.isDown(game.keys[id].left) then
game.pos[id].vx = game.pos[id].vx - accel*dt
game.pos[id].vy = game.pos[id].vy + accel*dt
game.direction[id] = -1
walking = true
end
if love.keyboard.isDown(game.keys[id].right) then
game.pos[id].vx = game.pos[id].vx + accel*dt
game.pos[id].vy = game.pos[id].vy - accel*dt
game.direction[id] = 1
walking = true
end
if love.keyboard.isDown(game.keys[id].up) then
game.pos[id].vx = game.pos[id].vx - accel*dt
game.pos[id].vy = game.pos[id].vy - accel*dt
walking = true
end
if love.keyboard.isDown(game.keys[id].down) then
game.pos[id].vx = game.pos[id].vx + accel*dt
game.pos[id].vy = game.pos[id].vy + accel*dt
walking = true
end
else
game.players[id].gui.update(dt,id)
end
if walking ~= game.players[id].walking then
local w = 0.5
local h = 0.5
local center_x = 0.5
local center_y = 1.0
local sprite_w = 32
local sprite_h = 64
local t_w, t_h = to_canvas_coord(w,h)
local offset_x = (-sprite_w)*center_x
local offset_y = (-sprite_h)*center_y
if walking == true then
set_sprite(id,"player_walk.png","1-10",player_num,0.1,1,sprite_w,sprite_h,offset_x, offset_y)
else
set_sprite(id,"player.png",1,player_num,0.2,game.direction[id],sprite_w,sprite_h,offset_x, offset_y)
end
game.players[id].walking = walking
end
-- check interactables within range
local closest = nil
local closest_dist = math.pow(18,2)
for i_id, interactable in pairs(game.interactables) do
--check that the object is on the same chunk as the player
local chunk_player_x, chunk_player_y = get_current_chunk(id)
local chunk_this_x, chunk_this_y = get_current_chunk(i_id)
if chunk_player_x == chunk_this_x and chunk_player_y == chunk_this_y then
--check that object is close
local dx = game.pos[i_id].x-game.pos[id].x
local dy = game.pos[i_id].y-game.pos[id].y
dx, dy = to_canvas_coord(dx,dy)
local distance = math.pow(dx,2) + math.pow(dy,2)
if interactable.active and ( distance < closest_dist ) then
if interactable.functions.interact ~= nil and interactable.functions.check_interact ~= nil then
if interactable.functions.check_interact(i_id,id) == true then
closest = i_id
closest_dist = distance
end
end
end
end
end
if game.players[id].available_interactable ~= closest then
local hl_sprite = load_resource("hilight_interactable.png","sprite")
game.players[id].hl_sprite = hl_sprite
local hl_grid = anim8.newGrid(42,32,hl_sprite:getWidth(),hl_sprite:getHeight())
game.players[id].hl_grid = hl_grid
game.players[id].hl_anim = anim8.newAnimation(hl_grid("4-1",player_num),0.05,'pauseAtEnd')
end
game.players[id].available_interactable = closest
if game.players[id].available_interactable ~= nil then
game.players[id].hl_anim:update(dt)
end
end
function update_plants(dt)
--growth is set so that
-- diffrent growing stages between 0 and 1
-- at 1 the plant is fully grown.
-- at 2 it is decomposing
for id,plant in pairs(game.plants) do
if plant.species == 'sunflower' then
local growth_rate = 0.0167 -- 1 minute to grow
if plant.growth > 0.3 and plant.state < 0.7 then
set_sprite(id,"plants.png",2,1,0.8,1,32,64,-16,-64+8)
plant.state = 1
end
if plant.growth > 0.7 and plant.state <= 1.0 then
set_sprite(id,"plants.png",3,1,0.8,1,32,64,-16,-64+8)
plant.state = 2
end
if plant.growth > 1.0 and plant.state <= 2 then
set_sprite(id,"plants.png",4,1,0.8,1,32,64,-16,-64+8)
plant.state = 3
end
plant.growth = plant.growth + dt*growth_rate
end
if plant.species == 'maize' then
local growth_rate = 0.03 --percent per second
if plant.growth > 0.3 and plant.state < 0.7 then
set_sprite(id,"plants.png",6,1,0.8,1,32,64,-16,-64+8)
plant.state = 1
end
if plant.growth > 0.7 and plant.state <= 1.0 then
set_sprite(id,"plants.png",7,1,0.8,1,32,64,-16,-64+8)
plant.state = 2
end
if plant.growth > 1.0 and plant.state <= 2 then
set_sprite(id,"plants.png",8,1,0.8,1,32,64,-16,-64+8)
plant.state = 3
end
plant.growth = plant.growth + dt*growth_rate
end
if plant.species == 'tulip' then
local growth_rate = 0.018 --percent per second
if plant.growth > 0.3 and plant.state < 0.7 then
set_sprite(id,"plants.png",10,3,0.8,1,32,32,-16,-32+8)
plant.state = 1
end
if plant.growth > 0.7 and plant.state <= 1.0 then
set_sprite(id,"plants.png",11,3,0.8,1,32,32,-16,-32+8)
plant.state = 2
end
if plant.growth > 1.0 and plant.state <= 2 then
set_sprite(id,"plants.png",12,3,0.8,1,32,32,-16,-32+8)
plant.state = 3
end
plant.growth = plant.growth + dt*growth_rate
end
if plant.species == 'berry_bush' then
local growth_rate = 0.01 --percent per second
if plant.growth > 0.3 and plant.state < 0.4 then
set_sprite(id,"plants.png",2,3,0.8,1,32,32,-16,-32+8)
plant.state = 1
end
if plant.growth > 0.4 and plant.state <= 1.0 then
set_sprite(id,"plants.png",3,3,0.8,1,32,32,-16,-32+8)
plant.state = 2
end
if plant.growth > 1.0 and plant.state <= 2 then
set_sprite(id,"plants.png",4,3,0.8,1,32,32,-16,-32+8)
plant.state = 3
end
plant.growth = plant.growth + dt*growth_rate
end
if plant.species == 'carrot' then
local growth_rate = 0.015 --percent per second
if plant.growth > 0.3 and plant.state < 0.7 then
set_sprite(id,"plants.png",6,3,0.8,1,32,32,-16,-32+8)
plant.state = 1
end
if plant.growth > 0.7 and plant.state <= 1.0 then
set_sprite(id,"plants.png",7,3,0.8,1,32,32,-16,-32+8)
plant.state = 2
end
if plant.growth > 1.0 and plant.state <= 2 then
set_sprite(id,"plants.png",8,3,0.8,1,32,32,-16,-32+8)
plant.state = 3
end
plant.growth = plant.growth + dt*growth_rate
end
end
end