Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- buttons = {
- left = 0,
- right = 1,
- up = 2,
- down = 3,
- z = 4,
- x = 5,
- }
- -- game
- -- 1,5 : game clocks
- gameclockmax = 5
- -- 6,20+: anim clocks
- animclockstart = 6
- animations = {
- -- animation blocks (index),
- -- animation size (w,h),
- -- animation length (blocks),
- -- animation speed (sec)
- -- animation flips (x,y)
- p_up = {7,1,1,3,0.12},
- p_down = {1,1,1,3,0.12},
- p_left = {4,1,1,3,0.12,true},
- p_right = {4,1,1,3,0.12},
- bat_fly = {20,1,1,3,0.15},
- }
- camx = 0
- camy = 0
- -- draws debug text
- debug = false
- -- create player, get animations working
- function smallprint(txt,x,y,col)
- local font = {
- ['a'] = 225,
- ['b'] = 226,
- ['c'] = 227,
- ['d'] = 228,
- ['e'] = 229,
- ['f'] = 230,
- ['g'] = 231,
- ['h'] = 232,
- ['i'] = 233,
- ['j'] = 234,
- ['k'] = 235,
- ['l'] = 236,
- ['m'] = 237,
- ['n'] = 238,
- ['o'] = 239,
- ['p'] = 240,
- ['q'] = 241,
- ['r'] = 242,
- ['s'] = 243,
- ['t'] = 244,
- ['u'] = 245,
- ['v'] = 246,
- ['w'] = 247,
- ['x'] = 248,
- ['y'] = 249,
- ['z'] = 250,
- }
- for i = 1, #txt do
- local sprite = font[sub(txt,i,i)]
- if(sprite != nil)then
- -- switch sprite colors with new ones
- pal(7,col)
- spr(sprite,x+((i-1)*6),y)
- pal()
- else
- print(sub(txt,i,i),x+((i-1)*6),y)
- end
- end
- end
- function _init()
- cls()
- -- game vars
- clocks = {}
- player = {
- x = 63,
- y = 63,
- xvel = 0,
- yvel = 0,
- fric = 3,
- spd = 0.75,
- damp = 1.5,
- width = 4,
- height = 7,
- init = function(self)
- load_animation(animations.p_down,1)
- pause_animation(1)
- end,
- draw = function(self)
- play_animation(self.x,self.y,1,1)
- end,
- update = function(self)
- local moved = false
- if(btn(buttons.up))then
- moved = true
- self.yvel -= self.spd/self.damp
- load_animation(animations.p_up,1)
- end
- if(btn(buttons.down))then
- moved = true
- self.yvel += self.spd/self.damp
- load_animation(animations.p_down,1)
- end
- if(btn(buttons.left))then
- moved = true
- self.xvel -= self.spd/self.damp
- load_animation(animations.p_left,1)
- end
- if(btn(buttons.right))then
- moved = true
- self.xvel += self.spd/self.damp
- load_animation(animations.p_right,1)
- end
- -- friction
- if(not btn(buttons.left) and not btn(buttons.right))then
- self.xvel /= self.fric
- end
- if(not btn(buttons.up) and not btn(buttons.down))then
- self.yvel /= self.fric
- end
- if(self.xvel > self.spd) self.xvel = self.spd
- if(self.xvel < -self.spd) self.xvel = -self.spd
- if(self.yvel > self.spd) self.yvel = self.spd
- if(self.yvel < -self.spd) self.yvel = -self.spd
- if(not checksolid(self.x+self.xvel,self.y,self.width,self.height))then
- self.x += self.xvel
- else
- self.xvel = 0
- end
- if(not checksolid(self.x,self.y+self.yvel,self.width,self.height))then
- self.y += self.yvel
- else
- self.yvel = 0
- end
- if(moved)then
- if(animation_ispaused(1)) resume_animation(1,0.12)
- else
- pause_animation(1)
- setframe_animation(1,1)
- end
- end,
- }
- animations_active = {
- frames = {}
- }
- game_init()
- end
- colchxmin = 0
- colchxmax = 0
- colchymin = 0
- colchymax = 0
- function checksolid(x,y,w,h)
- -- loop through every pixel corresponding to a possible cell
- -- /8 used because pixel map is 8 pixels.
- -- example
- -- from x=10,y=12,w=4,h=7
- -- 10-4 = (6/8)+3.0 = 3,
- -- 10+4 = 14 + 0.5 = 14:
- -- 12-7 = 6/8 + 0.5 = 1,
- -- 12+7 = 19-0.5 = 18
- -- checks:
- -- (3, 14)/8 = 0.375, 1.75
- -- (1, 18)/8 = 0.125, 2.25
- colchxmin = flr((x-w/8)+3.0)/8
- colchxmax = flr((x+w)+0.5)/8
- colchymin = flr((y-h/8)+0.5)/8
- colchymax = flr((y+h)-0.5)/8
- for x=flr((x-w/8)+3.0),flr((x+w)+0.5) do -- *8,-2.5+w
- for y=flr((y-h/8)+0.5),flr((y+h)-0.5) do -- *8,-1+h
- if fget(mget(x/8,y/8),1) then
- return true
- end
- end
- end
- return false
- end
- function load_animation(anim,slot)
- -- if we already have this animation, don't reset it!
- if(animations_active[slot] != anim) animations_active.frames[slot] = 1
- -- add animation to anim list
- animations_active[slot] = anim
- -- set frames
- end
- function animation_ispaused(slot)
- if( animations_active[slot] == nil) return false
- return animations_active[slot][5] == -1
- end
- function add_animation(anim)
- -- add animation to anim list
- animations_active[#animations_active+1] = anim
- -- set frames
- animations_active.frames[#animations_active.frames+1] = 1
- -- return where animation was loaded.
- return #animations_active
- end
- function unload_animation(slot)
- if(animations_active[slot] != nil)then
- -- use object here
- del(animations_active,animations_active[slot])
- -- because frames is a integeral value, use that just as is.
- del(animations_active.frames,slot)
- end
- end
- function play_animation(x,y,slot,loop)
- loop = loop or false
- -- animation structure in _init
- -- animation clock stored in clocks
- if(animations_active[slot] == nil)then
- -- animation not loaded!
- --print("no animation.",0,20,7)
- return false
- else
- -- load animation in
- anim = animations_active[slot]
- end
- -- pre-caution
- anim = anim or {0,0,0,0,0,0,0}
- local block, sizew,sizeh, length, speed, flipx, flipy = anim[1],anim[2],anim[3],anim[4],anim[5],anim[6],anim[7]
- -- make sure these are not nil!
- flipx = flipx or false
- flipy = flipy or false
- if(clocks[animclockstart+slot] == nil)then
- clocks[animclockstart+slot] = time()
- end
- if(speed != -1)then
- if( (time() - clocks[animclockstart+slot]) >= speed )then
- -- next frame
- if(animations_active.frames[slot] >= length)then
- -- loop frames are remove animation
- if(loop)then
- animations_active.frames[slot] = 1
- else
- unload_animation(slot)
- return true
- end
- else
- animations_active.frames[slot] += 1
- clocks[animclockstart+slot] = time()
- end
- end
- end
- -- draw animation
- spr(block+animations_active.frames[slot]-1,x,y,sizew,sizeh,flipx,flipy)
- end
- function pause_animation(slot)
- if(animations_active[slot] == nil) return false
- -- freeze animation clock
- animations_active[slot][5] = -1
- clocks[animclockstart+slot] = time()
- end
- function resume_animation(slot,spd)
- spd = spd or -1
- if(animations_active[slot] == nil) return false
- -- set animation clock
- animations_active[slot][5] = spd
- clocks[animclockstart+slot] = 0
- end
- function setframe_animation(slot,frame)
- if(animations_active.frames[slot] == nil) return false
- -- set animation clock
- animations_active.frames[slot] = frame
- clocks[animclockstart+slot] = 0
- end
- function game_init()
- -- fill screen with grass
- for y = 0, 32 do
- for x = 0, 32 do
- mset(x,y,10+flr(rnd(4)))
- end
- end
- -- 32 random blocks
- for i = 1, 32 do
- mset(flr(rnd(32)),flr(rnd(32)),14+flr(rnd(5)))
- end
- load_animation(animations.bat_fly,2)
- player:init()
- end
- function _draw()
- map(0,0,0,0,32,32)
- player:draw()
- play_animation(40,50,2,1)
- -- draw solids on-top of player.
- map(0,0,0,0,16,16,2)
- if(debug)then
- print("colx: (" ..colchxmin .. ", " .. colchxmax .. ")" ,0,0,7)
- print("coly: (" ..colchymin .. ", " .. colchymax .. ")" ,0,8,7)
- print("cell: " .. flr(player.x/8) .. ", " .. flr(player.y/8),0,16,7)
- else
- rectfill(camx,camy,127+camx,15+camy,0)
- print("pixel rpg demo",40+camx,10+camy,7)
- smallprint("welcome!",45+camx,0+camy,7)
- end
- end
- function _update60()
- player:update()
- camx = max(player.x-63,0)
- camy = max(player.y-63,0)
- if(camx > 127) camx = 127
- if(camy > 127) camy = 127
- -- cam change
- camer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement