Advertisement
Redxone

[PICO8] Animation Demo.

Aug 19th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.93 KB | None | 0 0
  1.  buttons = {
  2.    left  = 0,
  3.    right = 1,
  4.    up    = 2,
  5.    down  = 3,
  6.    z     = 4,
  7.    x     = 5,
  8.  }
  9. -- game
  10.  -- 1,5  : game clocks
  11.  gameclockmax = 5
  12.  -- 6,20+: anim clocks
  13.  animclockstart = 6
  14.  animations = {
  15.   -- animation blocks (index),
  16.   -- animation size   (w,h),
  17.   -- animation length (blocks),
  18.   -- animation speed  (sec)
  19.   -- animation flips  (x,y)
  20.   p_up    = {7,1,1,3,0.12},
  21.   p_down  = {1,1,1,3,0.12},
  22.   p_left  = {4,1,1,3,0.12,true},
  23.   p_right = {4,1,1,3,0.12},
  24.   bat_fly = {20,1,1,3,0.15},
  25.  }
  26.  
  27.  camx = 0
  28.  camy = 0
  29. -- draws debug text
  30.  debug = false
  31. -- create player, get animations working
  32.  
  33. function smallprint(txt,x,y,col)
  34.   local font = {
  35.     ['a'] = 225,
  36.     ['b'] = 226,
  37.     ['c'] = 227,
  38.     ['d'] = 228,
  39.     ['e'] = 229,
  40.     ['f'] = 230,
  41.     ['g'] = 231,
  42.     ['h'] = 232,
  43.     ['i'] = 233,
  44.     ['j'] = 234,
  45.     ['k'] = 235,
  46.     ['l'] = 236,
  47.     ['m'] = 237,
  48.     ['n'] = 238,
  49.     ['o'] = 239,
  50.     ['p'] = 240,
  51.     ['q'] = 241,
  52.     ['r'] = 242,
  53.     ['s'] = 243,
  54.     ['t'] = 244,
  55.     ['u'] = 245,
  56.     ['v'] = 246,
  57.     ['w'] = 247,
  58.     ['x'] = 248,
  59.     ['y'] = 249,
  60.     ['z'] = 250,
  61.   }
  62.   for i = 1, #txt do
  63.      local sprite = font[sub(txt,i,i)]
  64.      if(sprite != nil)then
  65.        -- switch sprite colors with new ones
  66.        pal(7,col)
  67.        spr(sprite,x+((i-1)*6),y)
  68.        pal()
  69.      else
  70.        print(sub(txt,i,i),x+((i-1)*6),y)
  71.      end
  72.   end
  73. end
  74.  
  75. function _init()
  76.  cls()
  77.  -- game vars
  78.  clocks = {}
  79.  player = {
  80.    x = 63,
  81.    y = 63,
  82.    xvel = 0,
  83.    yvel = 0,
  84.    fric = 3,
  85.    spd  = 0.75,
  86.    damp = 1.5,
  87.    width  = 4,
  88.    height = 7,
  89.  
  90.    init = function(self)
  91.      load_animation(animations.p_down,1)
  92.      pause_animation(1)
  93.    end,
  94.  
  95.    draw = function(self)
  96.      play_animation(self.x,self.y,1,1)
  97.    end,
  98.  
  99.    update = function(self)
  100.       local moved = false
  101.       if(btn(buttons.up))then
  102.          moved = true
  103.          self.yvel -= self.spd/self.damp
  104.          load_animation(animations.p_up,1)
  105.       end
  106.       if(btn(buttons.down))then
  107.          moved = true
  108.          self.yvel += self.spd/self.damp
  109.          load_animation(animations.p_down,1)
  110.       end
  111.       if(btn(buttons.left))then
  112.          moved = true
  113.          self.xvel -= self.spd/self.damp
  114.          load_animation(animations.p_left,1)
  115.       end
  116.       if(btn(buttons.right))then
  117.          moved = true
  118.          self.xvel += self.spd/self.damp
  119.          load_animation(animations.p_right,1)
  120.       end
  121.       -- friction
  122.       if(not btn(buttons.left) and not btn(buttons.right))then
  123.         self.xvel /= self.fric
  124.       end
  125.       if(not btn(buttons.up) and not btn(buttons.down))then
  126.         self.yvel /= self.fric
  127.       end
  128.      
  129.       if(self.xvel > self.spd) self.xvel = self.spd
  130.       if(self.xvel < -self.spd) self.xvel = -self.spd
  131.       if(self.yvel > self.spd) self.yvel = self.spd
  132.       if(self.yvel < -self.spd) self.yvel = -self.spd
  133.  
  134.       if(not checksolid(self.x+self.xvel,self.y,self.width,self.height))then
  135.          self.x += self.xvel
  136.       else
  137.          self.xvel = 0
  138.       end
  139.  
  140.       if(not checksolid(self.x,self.y+self.yvel,self.width,self.height))then
  141.          self.y += self.yvel
  142.       else
  143.          self.yvel = 0
  144.       end
  145.  
  146.  
  147.       if(moved)then
  148.         if(animation_ispaused(1)) resume_animation(1,0.12)
  149.       else
  150.          pause_animation(1)
  151.          setframe_animation(1,1)
  152.       end
  153.  
  154.    end,
  155.  
  156.  }
  157.  animations_active = {
  158.    frames = {}
  159.  }
  160.  game_init()
  161. end
  162.  
  163.  
  164. colchxmin = 0
  165. colchxmax = 0
  166. colchymin = 0
  167. colchymax = 0
  168. function checksolid(x,y,w,h)
  169.  -- loop through every pixel corresponding to a possible cell
  170.  -- /8 used because pixel map is 8 pixels.
  171.  -- example
  172.  -- from x=10,y=12,w=4,h=7
  173.  -- 10-4 = (6/8)+3.0 = 3,
  174.  -- 10+4 = 14 + 0.5  = 14:
  175.  -- 12-7 = 6/8 + 0.5 = 1,
  176.  -- 12+7 = 19-0.5    = 18
  177.  -- checks:
  178.  -- (3, 14)/8 = 0.375, 1.75
  179.  -- (1, 18)/8 = 0.125, 2.25
  180.  colchxmin = flr((x-w/8)+3.0)/8
  181.  colchxmax = flr((x+w)+0.5)/8
  182.  colchymin = flr((y-h/8)+0.5)/8
  183.  colchymax = flr((y+h)-0.5)/8
  184.  for x=flr((x-w/8)+3.0),flr((x+w)+0.5) do  -- *8,-2.5+w
  185.   for y=flr((y-h/8)+0.5),flr((y+h)-0.5) do -- *8,-1+h
  186.    if fget(mget(x/8,y/8),1) then
  187.      return true
  188.    end
  189.   end
  190.  end
  191.  return false
  192. end
  193.  
  194. function load_animation(anim,slot)
  195.  -- if we already have this animation, don't reset it!
  196.   if(animations_active[slot] != anim) animations_active.frames[slot] = 1
  197.   -- add animation to anim list
  198.   animations_active[slot] = anim
  199.   -- set frames
  200. end
  201.  
  202. function animation_ispaused(slot)
  203.    if( animations_active[slot] == nil) return false
  204.    return animations_active[slot][5] == -1
  205. end
  206.  
  207. function add_animation(anim)
  208.   -- add animation to anim list
  209.   animations_active[#animations_active+1] = anim
  210.   -- set frames
  211.   animations_active.frames[#animations_active.frames+1] = 1
  212.   -- return where animation was loaded.
  213.   return #animations_active
  214. end
  215.  
  216. function unload_animation(slot)
  217.   if(animations_active[slot] != nil)then
  218.      -- use object here
  219.      del(animations_active,animations_active[slot])
  220.      -- because frames is a integeral value, use that just as is.
  221.      del(animations_active.frames,slot)
  222.   end
  223. end
  224.  
  225. function play_animation(x,y,slot,loop)
  226.   loop = loop or false
  227.   -- animation structure in _init
  228.   -- animation clock stored in clocks
  229.   if(animations_active[slot] == nil)then
  230.     -- animation not loaded!
  231.     --print("no animation.",0,20,7)
  232.     return false
  233.   else
  234.     -- load animation in
  235.     anim = animations_active[slot]
  236.   end
  237.   -- pre-caution
  238.   anim = anim or {0,0,0,0,0,0,0}
  239.   local block, sizew,sizeh, length, speed, flipx, flipy = anim[1],anim[2],anim[3],anim[4],anim[5],anim[6],anim[7]
  240.   -- make sure these are not nil!
  241.   flipx = flipx or false
  242.   flipy = flipy or false
  243.   if(clocks[animclockstart+slot] == nil)then
  244.      clocks[animclockstart+slot] = time()
  245.   end
  246.   if(speed != -1)then
  247.    if( (time() - clocks[animclockstart+slot]) >= speed )then
  248.      -- next frame
  249.      if(animations_active.frames[slot] >= length)then
  250.       -- loop frames are remove animation
  251.       if(loop)then
  252.        animations_active.frames[slot] = 1
  253.       else
  254.        unload_animation(slot)
  255.        return true
  256.       end
  257.      else
  258.       animations_active.frames[slot] += 1
  259.       clocks[animclockstart+slot] = time()
  260.      end
  261.    end
  262.   end
  263.   -- draw animation
  264.   spr(block+animations_active.frames[slot]-1,x,y,sizew,sizeh,flipx,flipy)
  265. end
  266.  
  267. function pause_animation(slot)
  268.    if(animations_active[slot] == nil) return false
  269.    -- freeze animation clock
  270.    animations_active[slot][5] = -1
  271.    clocks[animclockstart+slot] = time()
  272. end
  273.  
  274. function resume_animation(slot,spd)
  275.    spd = spd or -1
  276.    if(animations_active[slot] == nil) return false
  277.    -- set animation clock
  278.    animations_active[slot][5] = spd
  279.    clocks[animclockstart+slot] = 0
  280. end
  281.  
  282. function setframe_animation(slot,frame)
  283.    if(animations_active.frames[slot] == nil) return false
  284.    -- set animation clock
  285.    animations_active.frames[slot] = frame
  286.    clocks[animclockstart+slot] = 0
  287. end
  288.  
  289.  
  290. function game_init()
  291.   -- fill screen with grass
  292.   for y = 0, 32 do
  293.      for x = 0, 32 do
  294.        mset(x,y,10+flr(rnd(4)))
  295.      end
  296.   end
  297.   -- 32 random blocks
  298.   for i = 1, 32 do
  299.    mset(flr(rnd(32)),flr(rnd(32)),14+flr(rnd(5)))
  300.   end
  301.   load_animation(animations.bat_fly,2)
  302.   player:init()
  303. end
  304.  
  305. function _draw()
  306.  map(0,0,0,0,32,32)
  307.  player:draw()
  308.  play_animation(40,50,2,1)
  309.  -- draw solids on-top of player.
  310.  map(0,0,0,0,16,16,2)
  311.  if(debug)then
  312.   print("colx: (" ..colchxmin .. ", " .. colchxmax .. ")" ,0,0,7)
  313.   print("coly: (" ..colchymin .. ", " .. colchymax .. ")" ,0,8,7)
  314.   print("cell: " .. flr(player.x/8) .. ", " .. flr(player.y/8),0,16,7)
  315.  else
  316.   rectfill(camx,camy,127+camx,15+camy,0)
  317.   print("pixel rpg demo",40+camx,10+camy,7)
  318.   smallprint("welcome!",45+camx,0+camy,7)
  319.  end
  320.  
  321. end
  322.  
  323.  
  324.  
  325. function _update60()
  326.   player:update()
  327.   camx = max(player.x-63,0)
  328.   camy = max(player.y-63,0)
  329.   if(camx > 127) camx = 127
  330.   if(camy > 127) camy = 127
  331.   -- cam change
  332.   camer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement