Sosowski

Gamer.as

Apr 6th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.geom.Point;
  4.     import net.flashpunk.Entity;
  5.     import net.flashpunk.graphics.Spritemap;
  6.    
  7.     // gamer is a classs fo a game entity, controlled either
  8.     // by local player, remote player or AI or nothing
  9.     public class Gamer extends Entity
  10.     {
  11.        
  12.         // variables
  13.         public var vx:Number = 0; // velocity
  14.         public var vy:Number = 0;
  15.         public var jumps:uint = max_jumps; // jumps performed
  16.        
  17.         // properties
  18.         public var speed:Number = 8.0; // max velocity
  19.         public var acc:Number = .2; // ecceleration
  20.         public var decc:Number = .98; // brakes
  21.         public var max_jumps:uint = 2; // x-iple jump
  22.         public var jumprate:Number = -12.0; // how high
  23.         public var bumprate:Number = 0.0; // wallfallbump rate
  24.         public var antigravity:Number = 1.0; // parachuting
  25.         public var fly:Boolean = false; // disregard for gravity
  26.         public var stationary:Boolean = false; // disregard for everything
  27.         public var manual:Boolean = false; // disregard for input()
  28.         static public var gravity:Number = .3; // gravity
  29.        
  30.         // BUTTONS
  31.         static public const UP:uint     = 0x01;
  32.         static public const DOWN:uint   = 0x02;
  33.         static public const LEFT:uint   = 0x04;
  34.         static public const RIGHT:uint  = 0x08;
  35.         static public const SHOOT:uint  = 0x10;
  36.         static public const JUMP:uint   = 0x20;
  37.         static public const SHIT:uint   = 0x40;
  38.         static public const FUCK:uint   = 0x80;
  39.         static public const MULTIJUMP:uint   = 0x100;
  40.        
  41.            
  42.         public function Gamer(x:Number,y:Number)
  43.         {
  44.             this.x = x;
  45.             this.y = y;
  46.         }
  47.        
  48.         // this return buttonpress bitmask, least 8 bits:
  49.         // 0 - UP  
  50.         // 1 - DOWN
  51.         // 2 - LEFT
  52.         // 3 - RIGHT
  53.         // 4 - SHOOT
  54.         // 5 - JUMP
  55.         // 6 - SOMETHING ELSE
  56.         // 7 - SOMETHING ELSER
  57.         public function input():uint
  58.         {
  59.             // please overload
  60.             return 0;
  61.         }
  62.        
  63.         override public function update():void
  64.         {
  65.             // if it doesn't move then... well it doesn't!
  66.             if (stationary) return;
  67.            
  68.             // if not controlled manually (by code) we check input
  69.             if (!manual)
  70.             {
  71.                 // get keys!
  72.                 var keys:uint = input();
  73.            
  74.                 // check left/right!
  75.                 if (keys & LEFT) vx -= acc;
  76.            
  77.                 if (keys & RIGHT) vx += acc;
  78.            
  79.                 // if flying, then not jumping :P
  80.                 if (fly)
  81.                 {
  82.                     if (keys & UP) vy -= acc;
  83.                    
  84.                     if (keys & DOWN) vy += acc;
  85.                
  86.                 } else {
  87.                 // else, totally jumping :P
  88.                     if ((keys & JUMP) && jumps)
  89.                     {
  90.                         vy = jumprate;
  91.                         jumps--;
  92.                     }
  93.                 }
  94.             }
  95.            
  96.             // collision detection assumes all objects initially do not collide
  97.            
  98.             // gravitizing
  99.             vy += gravity * antigravity;
  100.             // friction
  101.             vx *= decc;
  102.            
  103.             // ground / head collision
  104.             if (collide("level", x, y + vy))
  105.             {
  106.                
  107.                 if (vy < 0) // goes up
  108.                 {
  109.                     // hit the ceiling
  110.                     vy = 0;// -vy * bumprate;
  111.                 } else { // falling or standing
  112.                    
  113.                     // landed, we bump if necessary
  114.                     jumps = 0;
  115.                     vy = -vy * bumprate;
  116.                     jumps = max_jumps;
  117.                 }
  118.                
  119.                
  120.             }
  121.            
  122.             // side level collision
  123.             // please note the double collision check
  124.             if (collide("level", x+vx, y) || collide("level", x+vx, y+vy))
  125.             {
  126.                
  127.                 if (vx < 0) // goes up
  128.                 {
  129.                    
  130.                 } else { // falling or standing
  131.                    
  132.                 }
  133.                
  134.                 // bump if necessary (or stop)
  135.                 vx = -vx * bumprate;
  136.             }
  137.            
  138.             // apply velocity!
  139.             x += vx;
  140.             y += vy;
  141.            
  142.             // it's as simple as that!
  143.            
  144.            
  145.         }
  146.        
  147.     }
  148. }
Add Comment
Please, Sign In to add comment