cirnofarm/src/cirno.lua

132 lines
No EOL
2.7 KiB
Lua

--[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-14 21:05:28",revision=66]]
mouse_debug = true
w=480
h=300
function _init()
player={}
add(player, {
x=128,
y=128,
speed=2,
hflip=false,
spriteIndex=64,
move_x=0,
move_y=0,
cm=true, -- Collide with map tiles
cb=true, -- Collide with world bounds
draw=function(self)
spr(self.spriteIndex,self.x,self.y, self.hflip)
if (mouse_debug == true) then
local col = 8;
if (checkCollision(self.move_x,self.move_y) == true) then col = 7 end
pset(self.move_x,self.move_y,col)
end
end,
update=function(self)
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 (cmap(self) == false) then
self.x = self.move_x
self.y = self.move_y
--end
--if (checkCollision(col_x,col_y) == false) then
-- x = col_x
-- y = col_y
--end
end
})
tile_width = 16
tile_height = 16
end
function _draw()
cls(0)
--camera(x - 240, y - 135)
drawMap()
for p in all(player) do
p:draw()
end
debug_mouse()
end
function drawMap()
for i=2,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 checkCollision(x,y)
return fget(mget(x/tile_width,y/tile_height),0)
end
function debug_mouse()
local mx,my = mouse()
local x_offset = 5
local y_offset = 5
-- tile size
local tw=16
local th=16
-- 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
local sprite = mget(tile_x,tile_y)
local flag = fget(sprite)
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)
print(sprite,mx+x_offset,my+y_offset+8*2,10)
print(flag,mx+x_offset,my+y_offset+8+8*2,10)
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