mirror of
https://gitlab.com/MaddoScientisto/cirnofarm.git
synced 2026-06-01 09:45:34 +00:00
211 lines
No EOL
4.4 KiB
Lua
211 lines
No EOL
4.4 KiB
Lua
--[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-18 17:11:46",revision=281]]
|
|
--include("/cirnofarm/src/actor.lua")
|
|
last_coll=0
|
|
mouse_debug = true
|
|
w=480
|
|
h=300
|
|
tile_width = 16
|
|
tile_height = 16
|
|
asdf = {top=0, side=1}
|
|
LAYERS_COUNT=4
|
|
|
|
LAYERS = {
|
|
{index=4, name="background", render=true, render_objects=false, spawn_objects=false},
|
|
{index=3, name="solid", render=true, render_objects=false, spawn_objects=false},
|
|
{index=2, name="foreground", render=true, render_objects=true, spawn_objects=false},
|
|
{index=1, name="objects", render=false, render_objects=false, spawn_objects=true}
|
|
}
|
|
|
|
function _init()
|
|
|
|
player={}
|
|
add(player, {
|
|
x=128,
|
|
y=128,
|
|
w=16,
|
|
h=16,
|
|
speed=0.05,
|
|
hflip=false,
|
|
spriteIndex=64,
|
|
move_x=0,
|
|
move_y=0,
|
|
noclip=false,
|
|
cm=true, -- Collide with map tiles
|
|
cb=true, -- Collide with world bounds
|
|
draw=function(self)
|
|
spr(self.spriteIndex,self.x,self.y, self.hflip)
|
|
print(string.format("x:%.2f y:%.2f mx:%.2f my:%.2f coll:%d",self.x,self.y,
|
|
self.move_x,self.move_y,last_coll),0,0,1)
|
|
end,
|
|
update=function(self)
|
|
|
|
local hitbox_x = 4
|
|
local hitbox_y = 8
|
|
local hitbox_w = 6
|
|
local hitbox_h = 8
|
|
|
|
--self.move_x = self.x
|
|
--self.move_y = self.y
|
|
if (btn(0)) self.move_x -= self.speed self.hflip = true
|
|
if (btn(1)) self.move_x += self.speed self.hflip = false
|
|
if (btn(2)) self.move_y -= self.speed
|
|
if (btn(3)) self.move_y += self.speed
|
|
|
|
if (check_collision(self.x+self.move_x+hitbox_x,
|
|
self.y+hitbox_y,
|
|
hitbox_w,
|
|
hitbox_h)) then
|
|
if (not self.noclip) then self.move_x = 0 end
|
|
end
|
|
|
|
if (check_collision(self.x+hitbox_x,
|
|
self.y+self.move_y+hitbox_y,
|
|
hitbox_w,
|
|
hitbox_h)) then
|
|
if (not self.noclip) then self.move_y = 0 end
|
|
end
|
|
|
|
self.move_x *= 0.95
|
|
self.move_y *= 0.95
|
|
|
|
self.x += self.move_x
|
|
self.y += self.move_y
|
|
end
|
|
})
|
|
|
|
|
|
end
|
|
|
|
function _draw()
|
|
cls(0)
|
|
--camera(x - 240, y - 135)
|
|
|
|
foreach(LAYERS, render_layer)
|
|
--drawMap()
|
|
|
|
debug_mouse()
|
|
end
|
|
|
|
function render_layer(layer)
|
|
if (layer.render) then
|
|
map(fetch"map/0.map"[layer.index].bmp)
|
|
end
|
|
|
|
if (layer.render_objects) then
|
|
-- Render all objects here
|
|
for p in all(player) do
|
|
p:draw()
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
function drawMap()
|
|
for i=LAYERS_COUNT,1,-1 do
|
|
map(fetch"map/0.map"[i].bmp)
|
|
end
|
|
end
|
|
|
|
function _update()
|
|
for p in all(player) do
|
|
p:update()
|
|
end
|
|
end
|
|
|
|
function check_collision(x,y,w,h)
|
|
local collide = false
|
|
|
|
for i=x,x+w,w do
|
|
|
|
if (is_tile_solid(i,y) or is_tile_solid(i,y+h)) then
|
|
collide = collide or true
|
|
end
|
|
end
|
|
|
|
for i=y,y+h do
|
|
if (is_tile_solid(x,i) or is_tile_solid(x+w,i)) then
|
|
collide = collide or true
|
|
end
|
|
end
|
|
|
|
return collide
|
|
end
|
|
|
|
function get_layer_tile(x,y,layer)
|
|
return get(fetch"map/0.map"[layer].bmp,x,y)
|
|
end
|
|
|
|
function is_tile(tile_type,x,y)
|
|
--local tile = mget(x/tile_width,y/tile_height)
|
|
local tile = get_layer_tile(x/tile_width,y/tile_height,3)
|
|
local has_flag = fget(tile,tile_type)
|
|
--last_coll = fget(tile)
|
|
return has_flag
|
|
end
|
|
|
|
function is_tile_solid(x,y)
|
|
return is_tile(1,x,y) == 1
|
|
end
|
|
|
|
function can_move(x,y)
|
|
return is_tile(0,x,y)
|
|
end
|
|
|
|
function debug_mouse()
|
|
local mx,my = mouse()
|
|
local x_offset = 5
|
|
local y_offset = 5
|
|
|
|
-- tile size
|
|
local tw=tile_width
|
|
local th=tile_height
|
|
|
|
-- window width and height
|
|
local w=480
|
|
local h=270
|
|
|
|
-- offset if box leaves screen
|
|
if mx>w-20 then x_offset=-15 end
|
|
if my>h-29 then y_offset=-24 end
|
|
|
|
-- draw debug text box
|
|
local tile_x = mx\tw
|
|
local tile_y = my\th
|
|
|
|
rect((tile_x*tw)+tw,(tile_y*th)+th,tile_x*tw,(tile_y*th),8)
|
|
rectfill(mx+x_offset-1,my+y_offset-1,mx+x_offset+14,my+y_offset+23+8,1)
|
|
print(string.format("%d (%d)",tile_x,mx),mx+x_offset,my+y_offset,8)
|
|
print(string.format("%d (%d)",tile_y,my),mx+x_offset,my+y_offset+8,9)
|
|
|
|
for i=LAYERS_COUNT,1,-1 do
|
|
local sprite = get_layer_tile(tile_x,tile_y,i)
|
|
local flag = fget(sprite)
|
|
print(sprite,mx+x_offset+(i*16),my+y_offset+8*2,10)
|
|
print(flag,mx+x_offset+(i*16),my+y_offset+8+8*2,10)
|
|
end
|
|
end
|
|
|
|
function cmap(o)
|
|
local ct=false
|
|
local cb=false
|
|
|
|
-- if colliding with map tiles
|
|
if(o.cm) then
|
|
local x1=o.x/tile_width
|
|
local y1=o.y/tile_height
|
|
local x2=(o.x+tile_width-1)/tile_width
|
|
local y2=(o.y+tile_height-1)/tile_height
|
|
local a=fget(mget(x1,y1),1)
|
|
local b=fget(mget(x1,y2),1)
|
|
local c=fget(mget(x2,y2),1)
|
|
local d=fget(mget(x2,y1),1)
|
|
ct=a or b or c or d
|
|
end
|
|
-- if colliding world bounds
|
|
if(o.cw) then
|
|
cb=(o.x<0 or o.x+tile_width>w or
|
|
o.y<0 or o.y+tile_height>h)
|
|
end
|
|
|
|
return ct or cb
|
|
end |