Advertisement
spacechase0

Crate Pseudocode

Jun 28th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. Player
  2. {
  3.     update()
  4.     {
  5.         // ...
  6.         if ( bumped into a solid )
  7.         {
  8.             velocity = 0;
  9.             if ( bumped into CratePlaceholderBlock )
  10.             {
  11.                 CratePlaceholderBlock crate = ( CratePlaceholderBlock ) tile bumped into;
  12.                 crate.crate.bump( side bumped into );
  13.             }
  14.         }
  15.         // ...
  16.     }
  17. }
  18.  
  19. CratePlaceholderBlock
  20. {
  21.     CrateEntity crate;
  22.     isSolid() { return true; }
  23. }
  24.  
  25. CrateEntity
  26. {
  27.     Map< Position, Block > oldBlocks;
  28.     update()
  29.     {
  30.         // ...
  31.         // Similar code to what player has - Maybe common entity code?
  32.         for ( every tile the crate overlaps right now )
  33.         {
  34.             Block block = level.getBlockAt( ... );
  35.             if ( !( block instanceof CratePlaceholderBlock ) )
  36.             {
  37.                 level.setBlockAt( ..., new CratePlaceholderBlock( this ) );
  38.                 oldBlocks.put( ..., block );
  39.             }
  40.         }
  41.         for ( everything in oldBlocks )
  42.         {
  43.             Block block = ...;
  44.             if ( no longer overlapping that block )
  45.             {
  46.                 level.setBlockAt( ..., block );
  47.             }
  48.         }
  49.         // ...
  50.     }
  51.    
  52.     draw()
  53.     {
  54.         // Draw everything in oldBlocks so things don't flicker
  55.     }
  56.    
  57.     bump( Side side )
  58.     {
  59.         if ( opposite side does not have solid tile )
  60.         {
  61.             velocity = ...;
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement