Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.psillicoder.towerdefense1.entities;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.graphics.g2d.TextureRegion;
- import com.badlogic.gdx.math.Vector2;
- public class Enemy {
- private int enemyType;
- private int enemyDir;
- private final int width = 32;
- private final int height = 32;
- private final int tileSize = 32;
- private int x;
- private int y;
- //Vector position
- private Vector2 position;
- //Destination x/y for move
- private int destx,desty;
- private Vector2 destination;
- //Direction x/y
- private float dx, dy;
- private Vector2 dirVec;
- private float speed = 1f;
- private TextureRegion idleFrame;
- private Texture enemyTexture;
- public static enum State {
- IDLE,
- WALKING,
- }
- private State state;
- public Enemy(int t, int tx, int ty, int dir) {
- enemyType = t;
- enemyDir = dir;
- dirVec = new Vector2();
- position = new Vector2();
- destination = new Vector2();
- state = state.IDLE;
- position = new Vector2(tx,ty);
- if (enemyType == 0)enemyTexture = new Texture(Gdx.files.internal("crocenemy.png"));
- if (enemyType == 1)enemyTexture = new Texture(Gdx.files.internal("octoenemy.png"));
- if (enemyType == 2)enemyTexture = new Texture(Gdx.files.internal("shroomenemy.png"));
- idleFrame = new TextureRegion(enemyTexture,0,0,64,64);
- }
- public void moveTo(int px, int py) {
- state = state.WALKING;
- destination = new Vector2(px,py);
- dirVec.x = px - x;
- dirVec.y = py - y;
- float length = (float) Math.sqrt(dirVec.x*dirVec.x+dirVec.y*dirVec.y);
- dirVec.x=dirVec.x/length;
- dirVec.y=dirVec.y/length;
- dirVec = destination.cpy().sub(position).nor().scl(speed);
- }
- public void update(float delta) {
- if (state == state.WALKING) {
- if (position.x >= (destination.x - .25) && (position.x <= destination.x + .25) ) {
- if (position.y >= (destination.y - .25) && (position.y <= destination.y + .25) ) {
- position.x = destination.x;
- position.y = destination.y;
- state = state.IDLE;
- }
- }
- position.add(dirVec.cpy().scl(delta));
- System.out.println("x: " + position.x +
- " y: " + position.y +
- " dirVec: " + dirVec +
- " Destination: " + destination);
- }
- //System.out.println("X: " + x + " Y: " + y);
- }
- private boolean calculatePath() {
- return false;
- }
- public void render(SpriteBatch batch, float delta) {
- update(delta);
- batch.draw(idleFrame,position.x * tileSize ,position.y * tileSize ,width,height);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement