Advertisement
Guest User

Entity.java

a guest
Mar 20th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.12 KB | None | 0 0
  1. package com.vali.lib;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.graphics.Texture;
  5. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  6. import com.badlogic.gdx.math.MathUtils;
  7. import com.badlogic.gdx.math.Vector2;
  8.  
  9. public class Entity {
  10.    
  11.     public Texture tex;
  12.     public float x;
  13.     public float y;
  14.     public Vector2 acceleration;
  15.     public Vector2 velocity;
  16.     public Vector2 drag;
  17.     public Vector2 maxVelocity;
  18.     public Vector2 origin;
  19.     public float width;
  20.     public float height;
  21.     public float rotation;
  22.     public float offsetX;
  23.     public float offsetY;
  24.    
  25.     public float centerX;
  26.     public float centerY;
  27.    
  28.     public boolean col_top;
  29.     public boolean col_bot;
  30.     public boolean col_left;
  31.     public boolean col_right;
  32.    
  33.     // DONT MODIFY THESE //
  34.     public static int TOP = 0;
  35.     public static int RIGHT = 1;
  36.     public static int BOT = 2;
  37.     public static int LEFT = 3;
  38.     // END OF RESTRICTION //
  39.    
  40.     public State currentState;
  41.    
  42.     Animation currentAnimation;
  43.     private boolean _flipX;
  44.     private boolean _flipY;
  45.     private float _scaleX;
  46.     private float _scaleY;
  47.    
  48.     public boolean solid;
  49.    
  50.     String name;
  51.     public String tag;
  52.     public boolean visible = true;
  53.     public boolean alive;
  54.     private boolean in_path = false;
  55.    
  56.    
  57.     public float getX(){
  58.         return x;
  59.     }
  60.     public float getY(){
  61.         return y;
  62.     }
  63.     public void setX(float x){
  64.         this.x = x;
  65.     }
  66.     public void setY(float y){
  67.         this.y = y;
  68.     }
  69.    
  70.     public Entity(float XX,float YY, String path, float spriteWidth, float spriteHeight){
  71.         x = XX;
  72.         y = YY;
  73.        
  74.         tag = "untagged";
  75.        
  76.         tex = new Texture(Gdx.files.internal(path));
  77.         solid = true;
  78.         acceleration = new Vector2();
  79.        
  80.         velocity = new Vector2();
  81.        
  82.         _scaleX = 1;
  83.         _scaleY = 1;
  84.        
  85.         drag = new Vector2();
  86.        
  87.         maxVelocity = new Vector2();
  88.        
  89.         width = spriteWidth;
  90.         height = spriteHeight;
  91.        
  92.         alive = true;
  93.         origin = new Vector2();
  94.         origin.x = x + width / 2;
  95.         origin.y = y + height / 2;
  96.        
  97.         centerX = 0;
  98.         centerY = 0;
  99.        
  100.         col_bot = false;
  101.         col_top = false;
  102.         col_right = false;
  103.         col_left = false;
  104.        
  105.         _flipX = true;
  106.         _flipY = false;
  107.        
  108.     }
  109.     public void update(){
  110.         updateMotion();
  111.         updateAnimation();
  112.     }
  113.     public void updateSelf(){
  114.         updateMotion();
  115.         updateAnimation();
  116.     }
  117.     protected void updateAnimation(){
  118.         if(currentAnimation != null){
  119.             currentAnimation.update();
  120.         }
  121.     }
  122.     protected void updateMotion(){
  123.         velocity.x = MathUtils.clamp(velocity.x, -maxVelocity.x, maxVelocity.x);
  124.         velocity.y = MathUtils.clamp(velocity.y, -maxVelocity.y, maxVelocity.y);
  125.        
  126.         float d = 0;
  127.         d = drag.x * Gdx.graphics.getDeltaTime();
  128.         if(velocity.x - d > 0)
  129.             velocity.x -= d;
  130.         else if(velocity.x + d < 0)
  131.             velocity.x += d;
  132.         else
  133.             velocity.x = 0;
  134.        
  135.         d = drag.y * Gdx.graphics.getDeltaTime();
  136.         if(velocity.y - d > 0)
  137.             velocity.y -= d;
  138.         else if(velocity.y + d < 0)
  139.             velocity.y += d;
  140.         else
  141.             velocity.y = 0;
  142.        
  143.         origin.x = x + width / 2;
  144.         origin.y = y + height / 2;
  145.        
  146.         x += velocity.x * Gdx.graphics.getDeltaTime();
  147.         y += velocity.y * Gdx.graphics.getDeltaTime();
  148.     }
  149.     public void Collide(String TAG) {
  150.         if(Collision.place_entity(x + velocity.x * Gdx.graphics.getDeltaTime(), y ,width,height, TAG)){
  151.             while(!Collision.place_entity(x + Math.signum(velocity.x), y,width,height, TAG))
  152.                 x += Math.signum(velocity.x);
  153.             velocity.x = 0;
  154.         }
  155.        
  156.         if(Collision.place_entity(x, y + velocity.y * Gdx.graphics.getDeltaTime(), width, height,TAG)){
  157.             while(!Collision.place_entity(x, y + Math.signum(velocity.y) ,width, height , TAG))
  158.                 y += Math.signum(velocity.y);
  159.             velocity.y = 0;
  160.         }
  161.        
  162.     }
  163.     public void setFacingX(boolean FLIP){
  164.         _flipX = FLIP;
  165.     }
  166.     public void setFacingY(boolean FLIP){
  167.         _flipY = FLIP;
  168.     }
  169.     public void playAnimation(Animation anim){
  170.         if(currentAnimation != anim)
  171.         {
  172.             currentAnimation = anim;
  173.         }
  174.     }
  175.     public void setScaleX(float scale){
  176.         _scaleX = scale;
  177.     }
  178.     public void setScaleY(float scale){
  179.         _scaleY = scale;
  180.     }
  181.     public float getScaleX(){
  182.         return _scaleX;
  183.     }
  184.     public float getScaleY(){
  185.         return _scaleY;
  186.     }
  187.     public boolean isTouching(int Direction){
  188.         if(Direction == TOP)
  189.             return col_top;
  190.         if(Direction == BOT)
  191.             return col_bot;
  192.         if(Direction == LEFT)
  193.             return col_left;
  194.         if(Direction == RIGHT)
  195.             return col_right;
  196.         return false;
  197.     }
  198.     public void drawSelf(SpriteBatch sb){
  199.         if(currentAnimation != null)
  200.             sb.draw(tex, x, y, (float)currentAnimation.getSpriteWidth() / 2, (float)currentAnimation.getSpriteHeight() / 2, (float)currentAnimation.getSpriteWidth(), (float)currentAnimation.getSpriteHeight(), _scaleX, _scaleY, rotation, currentAnimation.getSpriteFrame(), 0, (int)currentAnimation.getSpriteWidth(), (int)currentAnimation.getSpriteHeight(), _flipX, _flipY);
  201.         else{
  202.             sb.draw(tex,x,y,centerX, centerY,(float)tex.getWidth(), (float)tex.getHeight(),_scaleX,_scaleY,rotation,0,0,tex.getWidth(),tex.getHeight(),_flipX,_flipY);
  203.         }
  204.     }
  205.     public void draw(SpriteBatch sb){
  206.         drawSelf(sb);
  207.     }
  208.     public static Animation addAnimation(Texture tex, int[] FRAMES, int speed, boolean LOOP, int spriteWidth, int spriteHeight){
  209.         return new Animation(tex,FRAMES,speed,LOOP,spriteWidth, spriteHeight,null);
  210.     }
  211.    
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement