cirnofarm/src/cirno.lua

211 lines
4.4 KiB
Lua
Raw Normal View History

2024-04-18 19:12:05 +02:00
--[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-18 17:11:46",revision=281]]
2024-04-15 20:07:29 +02:00
--include("/cirnofarm/src/actor.lua")
2024-04-17 00:08:46 +02:00
last_coll=0
2024-04-14 17:22:50 +02:00
mouse_debug = true
2024-04-14 23:11:08 +02:00
w=480
h=300
2024-04-17 00:08:46 +02:00
tile_width = 16
tile_height = 16
2024-04-15 20:07:29 +02:00
asdf = {top=0, side=1}
2024-04-18 19:12:05 +02:00
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}
}
2024-04-14 15:51:45 +02:00
function _init()
2024-04-15 20:07:29 +02:00
2024-04-14 21:13:54 +02:00
player={}
add(player, {
x=128,
y=128,
2024-04-16 00:01:26 +02:00
w=16,
h=16,
2024-04-17 00:08:46 +02:00
speed=0.05,
2024-04-14 21:13:54 +02:00
hflip=false,
spriteIndex=64,
2024-04-14 23:11:08 +02:00
move_x=0,
move_y=0,
2024-04-17 00:08:46 +02:00
noclip=false,
2024-04-16 00:01:26 +02:00
cm=true, -- Collide with map tiles
2024-04-14 23:11:08 +02:00
cb=true, -- Collide with world bounds
2024-04-14 21:13:54 +02:00
draw=function(self)
spr(self.spriteIndex,self.x,self.y, self.hflip)
2024-04-17 00:08:46 +02:00
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)
2024-04-14 21:13:54 +02:00
end,
2024-04-15 00:06:10 +02:00
update=function(self)
2024-04-17 11:54:48 +02:00
local hitbox_x = 4
local hitbox_y = 8
local hitbox_w = 6
local hitbox_h = 8
2024-04-17 00:08:46 +02:00
--self.move_x = self.x
--self.move_y = self.y
2024-04-14 23:11:08 +02:00
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
2024-04-17 11:54:48 +02:00
if (check_collision(self.x+self.move_x+hitbox_x,
self.y+hitbox_y,
hitbox_w,
hitbox_h)) then
2024-04-17 11:29:08 +02:00
if (not self.noclip) then self.move_x = 0 end
2024-04-17 00:08:46 +02:00
end
2024-04-17 11:54:48 +02:00
if (check_collision(self.x+hitbox_x,
self.y+self.move_y+hitbox_y,
hitbox_w,
hitbox_h)) then
2024-04-17 00:08:46 +02:00
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
2024-04-17 11:29:08 +02:00
self.y += self.move_y
2024-04-14 21:13:54 +02:00
end
})
2024-04-17 00:08:46 +02:00
2024-04-14 15:51:45 +02:00
end
function _draw()
cls(0)
2024-04-14 17:22:50 +02:00
--camera(x - 240, y - 135)
2024-04-14 15:51:45 +02:00
2024-04-18 19:12:05 +02:00
foreach(LAYERS, render_layer)
--drawMap()
2024-04-14 21:13:54 +02:00
2024-04-18 19:12:05 +02:00
debug_mouse()
end
function render_layer(layer)
if (layer.render) then
map(fetch"map/0.map"[layer.index].bmp)
2024-04-14 21:13:54 +02:00
end
2024-04-14 16:19:38 +02:00
2024-04-18 19:12:05 +02:00
if (layer.render_objects) then
-- Render all objects here
for p in all(player) do
p:draw()
end
end
2024-04-14 16:19:38 +02:00
end
2024-04-14 15:51:45 +02:00
2024-04-14 16:19:38 +02:00
function drawMap()
2024-04-18 19:12:05 +02:00
for i=LAYERS_COUNT,1,-1 do
2024-04-14 16:19:38 +02:00
map(fetch"map/0.map"[i].bmp)
end
2024-04-14 15:51:45 +02:00
end
function _update()
2024-04-14 21:13:54 +02:00
for p in all(player) do
p:update()
2024-04-14 17:22:50 +02:00
end
2024-04-14 16:19:38 +02:00
end
2024-04-17 11:29:08 +02:00
function check_collision(x,y,w,h)
local collide = false
2024-04-17 00:08:46 +02:00
for i=x,x+w,w do
2024-04-17 11:29:08 +02:00
if (is_tile_solid(i,y) or is_tile_solid(i,y+h)) then
collide = collide or true
end
2024-04-17 00:08:46 +02:00
end
2024-04-17 11:29:08 +02:00
for i=y,y+h do
if (is_tile_solid(x,i) or is_tile_solid(x+w,i)) then
collide = collide or true
2024-04-17 00:08:46 +02:00
end
end
2024-04-17 11:29:08 +02:00
2024-04-17 00:08:46 +02:00
return collide
2024-04-16 00:01:26 +02:00
end
2024-04-18 19:12:05 +02:00
function get_layer_tile(x,y,layer)
return get(fetch"map/0.map"[layer].bmp,x,y)
end
2024-04-16 00:01:26 +02:00
function is_tile(tile_type,x,y)
2024-04-18 19:12:05 +02:00
--local tile = mget(x/tile_width,y/tile_height)
local tile = get_layer_tile(x/tile_width,y/tile_height,3)
2024-04-16 00:01:26 +02:00
local has_flag = fget(tile,tile_type)
2024-04-17 00:08:46 +02:00
--last_coll = fget(tile)
2024-04-16 00:01:26 +02:00
return has_flag
end
2024-04-17 00:08:46 +02:00
function is_tile_solid(x,y)
2024-04-17 11:29:08 +02:00
return is_tile(1,x,y) == 1
2024-04-17 00:08:46 +02:00
end
2024-04-16 00:01:26 +02:00
2024-04-17 00:08:46 +02:00
function can_move(x,y)
return is_tile(0,x,y)
2024-04-14 17:22:50 +02:00
end
function debug_mouse()
local mx,my = mouse()
local x_offset = 5
local y_offset = 5
-- tile size
2024-04-17 00:08:46 +02:00
local tw=tile_width
local th=tile_height
2024-04-14 17:22:50 +02:00
-- 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
2024-04-14 16:19:38 +02:00
2024-04-14 17:22:50 +02:00
-- draw debug text box
2024-04-14 23:11:08 +02:00
local tile_x = mx\tw
local tile_y = my\th
2024-04-18 19:12:05 +02:00
2024-04-14 23:11:08 +02:00
rect((tile_x*tw)+tw,(tile_y*th)+th,tile_x*tw,(tile_y*th),8)
2024-04-14 17:22:50 +02:00
rectfill(mx+x_offset-1,my+y_offset-1,mx+x_offset+14,my+y_offset+23+8,1)
2024-04-14 23:11:08 +02:00
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)
2024-04-18 19:12:05 +02:00
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
2024-04-14 23:11:08 +02:00
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
2024-04-14 15:51:45 +02:00
end