Advertisement
Dr_Davenstein

Simple Animated Tile Example

Aug 29th, 2021 (edited)
3,714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include once "fbgfx.bi"
  2. screenres 1024, 768, 32
  3.  
  4.  
  5.  
  6. type base_tile
  7.  
  8.     as string*1 texture
  9.  
  10. end type
  11.  
  12.  
  13. type advanced_tile extends base_tile
  14.  
  15.     as integer isAnimated
  16.  
  17.     as string*1 textures(any)
  18.  
  19.     as double timed
  20.  
  21.     as integer curTile
  22.  
  23.     declare sub update()
  24.  
  25. end type
  26.  
  27.  
  28. declare sub init( tiles() as advanced_tile )
  29.  
  30.  
  31.  
  32. dim as advanced_tile tiles(49,49)
  33.  
  34. init( tiles() )
  35.  
  36.  
  37.  
  38.  
  39. do
  40.  
  41.     'update loop
  42.     'always call an update in a separate loop like this, before drawing
  43.     for y as integer = 0 to ubound(tiles,2)
  44.  
  45.         for x as integer = 0 to ubound(tiles,1)
  46.  
  47.             tiles(x,y).update()
  48.  
  49.         next
  50.  
  51.     next
  52.  
  53.  
  54.     'drawing loop
  55.     'never call the update function in this loop,
  56.     'as it may display incorrect data to the player
  57.     screenlock
  58.  
  59.     cls
  60.  
  61.     for y as integer = 0 to ubound(tiles,2)
  62.  
  63.         for x as integer = 0 to ubound(tiles,1)
  64.  
  65.             draw string (x*8,y*8), tiles(x,y).texture
  66.  
  67.         next
  68.  
  69.     next
  70.  
  71.     screensync
  72.  
  73.     screenunlock
  74.  
  75.     sleep 3,1
  76.  
  77. loop until multikey(FB.SC_ESCAPE)
  78.  
  79.  
  80.  
  81. sub advanced_tile.update()
  82.  
  83.  
  84.     if isAnimated then
  85.  
  86.         if timer>timed then
  87.  
  88.             timed = timer+rnd*5
  89.             curTile+=1
  90.  
  91.             if curTile>ubound(textures) then
  92.  
  93.                 curTile = 0
  94.  
  95.             end if
  96.  
  97.             texture = textures(curTile)
  98.  
  99.         end if
  100.  
  101.     else
  102.  
  103.         texture = textures(0)
  104.  
  105.     end if
  106.  
  107.  
  108. end sub
  109.  
  110.  
  111. sub init( tiles() as advanced_tile )
  112.  
  113.  
  114.     for y as integer = 0 to ubound(tiles,2)
  115.  
  116.         for x as integer = 0 to ubound(tiles,1)
  117.  
  118.             with tiles(x,y)
  119.  
  120.                 if x = 0 or y = 0 or x = ubound(tiles, 1) or y = ubound(tiles, 2) then
  121.  
  122.                     .isAnimated = false
  123.                     redim .textures(0)
  124.                     .textures(0) = chr(219)
  125.  
  126.                 else
  127.  
  128.                     if int(rnd*3) = 0 then
  129.  
  130.                         .isAnimated = true
  131.                         redim .textures(2)
  132.                         .textures(0) = chr(178)
  133.                         .textures(1) = chr(177)
  134.                         .textures(2) = chr(176)
  135.  
  136.                     else
  137.  
  138.                         .isAnimated = false
  139.                         redim .textures(0)
  140.                         .textures(0) = chr(219)
  141.  
  142.                     end if
  143.  
  144.                 end if
  145.  
  146.             end with
  147.  
  148.         next
  149.  
  150.     next
  151.  
  152.  
  153. end sub
  154.  
  155.  
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement