SHARE
TWEET

Enemy.java

a guest Jan 7th, 2017 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.psillicoder.towerdefense1.entities;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.graphics.Texture;
  5. import com.badlogic.gdx.graphics.g2d.Sprite;
  6. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  7. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  8. import com.badlogic.gdx.math.Vector2;
  9.  
  10. public class Enemy {
  11.    
  12.     private int enemyType;
  13.    
  14.     private int enemyDir;
  15.     private final int width = 32;
  16.     private final int height = 32;
  17.    
  18.     private final int tileSize = 32;
  19.    
  20.     private int x;
  21.     private int y;
  22.    
  23.     //Vector position
  24.     private Vector2 position;
  25.    
  26.     //Destination x/y for move
  27.     private int destx,desty;
  28.     private Vector2 destination;
  29.    
  30.    
  31.     //Direction x/y
  32.     private float dx, dy;
  33.     private Vector2 dirVec;
  34.    
  35.     private float speed = 1f;
  36.    
  37.     private TextureRegion idleFrame;
  38.    
  39.     private Texture enemyTexture;
  40.    
  41.     public static enum State {
  42.         IDLE,
  43.         WALKING,
  44.     }
  45.    
  46.     private State state;
  47.    
  48.     public Enemy(int t, int tx, int ty, int dir) {
  49.         enemyType = t;
  50.         enemyDir = dir;
  51.        
  52.         dirVec = new Vector2();
  53.         position = new Vector2();
  54.         destination = new Vector2();
  55.        
  56.         state = state.IDLE;
  57.        
  58.         position = new Vector2(tx,ty);
  59.        
  60.         if (enemyType == 0)enemyTexture = new Texture(Gdx.files.internal("crocenemy.png"));
  61.         if (enemyType == 1)enemyTexture = new Texture(Gdx.files.internal("octoenemy.png"));
  62.         if (enemyType == 2)enemyTexture = new Texture(Gdx.files.internal("shroomenemy.png"));
  63.        
  64.         idleFrame = new TextureRegion(enemyTexture,0,0,64,64);
  65.        
  66.        
  67.     }
  68.    
  69.     public void moveTo(int px, int py) {
  70.         state = state.WALKING;
  71.        
  72.         destination = new Vector2(px,py);
  73.        
  74.         dirVec.x = px - x;
  75.         dirVec.y = py - y;
  76.        
  77.         float length = (float) Math.sqrt(dirVec.x*dirVec.x+dirVec.y*dirVec.y);
  78.        
  79.         dirVec.x=dirVec.x/length;
  80.         dirVec.y=dirVec.y/length;
  81.        
  82.         dirVec = destination.cpy().sub(position).nor().scl(speed);
  83.     }
  84.    
  85.    
  86.    
  87.     public void update(float delta) {
  88.        
  89.        
  90.             if (state == state.WALKING) {
  91.    
  92.                
  93.             if (position.x >= (destination.x - .25) && (position.x <= destination.x + .25) ) {
  94.                 if (position.y >= (destination.y - .25) && (position.y <= destination.y + .25) ) {
  95.                     position.x = destination.x;
  96.                     position.y = destination.y;
  97.                     state = state.IDLE;
  98.                 }
  99.             }
  100.    
  101.                
  102.                 position.add(dirVec.cpy().scl(delta));
  103.                
  104.             System.out.println("x: " + position.x +
  105.                     " y: " + position.y +
  106.                     " dirVec: " + dirVec +
  107.                     " Destination: " + destination);
  108.            
  109.             }
  110.         //System.out.println("X: " + x + " Y: " + y);
  111.     }
  112.    
  113.    
  114.     private boolean calculatePath() {
  115.         return false;
  116.     }
  117.    
  118.    
  119.     public void render(SpriteBatch batch, float delta) {
  120.         update(delta);
  121.        
  122.         batch.draw(idleFrame,position.x * tileSize ,position.y * tileSize ,width,height);
  123.     }
  124.  
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top