-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.lua
78 lines (69 loc) · 2.05 KB
/
common.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
function sign(x)
return (x<0 and -1) or 1
end
function get_current_inv_id(player_id)
local active_slot = game.players[player_id].active_inventory_slot
return game.players[player_id].inventory[active_slot]
end
function next_inventory_item(player_id)
local player = game.players[player_id]
if player.active_inventory_slot < player.inventory_slots then
player.active_inventory_slot = player.active_inventory_slot + 1
else
player.active_inventory_slot = 1
end
end
function get_tile_and_tileset(x,y,layer)
x = math.floor(x)
y = math.floor(y)
local tile = layer.data[x+(y-1)*layer.width]
if tile ~= nil then
tile = tile - 1
local tileset = nil
for _,ts in pairs(game.map.tilesets) do
if(tile < ts.lastgid) then
tileset = ts
break
end
end
if(tileset ~= nil) then
return tile,tileset
end
else
return nil, nil
end
end
function set_tile(x,y,layer,tile)
x = math.floor(x)
y = math.floor(y)
layer.data[x+(y-1)*layer.width] = tile+1
end
function to_tile_coord(x,y,truncate)
if truncate == nil then truncate = true end
local ret_x = x/game.map.tilewidth + y/game.map.tileheight
local ret_y = -x/game.map.tilewidth + y/game.map.tileheight
if truncate then
return math.floor(ret_x),math.floor(ret_y)
else
return ret_x,ret_y
end
end
function to_canvas_coord(x,y)
local ret_x = (x-y)*game.tile_size.w*0.5
local ret_y = (x+y)*game.tile_size.h*0.5
return ret_x,ret_y
end
function get_current_chunk(player_id)
local player_tile_x, player_tile_y = game.pos[player_id].x, game.pos[player_id].y
local x = math.floor((player_tile_x-1)/game.chunksize)
local y = math.floor((player_tile_y-1)/game.chunksize)
return x,y
end
function play_sound(name)
print(name..".wav")
local sound = load_resource(name..".wav","sfx"),
print(sound)
-- randomize pitch
sound:setPitch(1 - 0.5*(math.random()-0.5))
love.audio.play(sound)
end