Java tutorial
// Copyright 2015 Octavio Galland // // This file is part of TopDown // // TopDown is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. package com.octa.topdown.entities; import java.util.Random; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.math.Vector2; import com.octa.topdown.map.TileMap; import com.octa.topdown.system.SinCosTable; public class Zombie extends Entity { private int damage; private Vector2 objective; private long lastDamaged; private float cos; private float sin; float hp; public static int animId; private TileMap map; private boolean chaseObj; public Zombie() { super(animId, 0, 0, 60, 60); knock = new Vector2(); } public Zombie(int x, int y, int width, int height) { super(animId, x, y, width, height); this.bbDif = 4; updateAABB(); this.objective = new Vector2(0, 0); this.damage = 1; this.speed = 150; this.hp = 100; this.lastDamaged = 0; chaseObj = true; knock = new Vector2(); } public void init(TileMap map) { this.bbDif = 4; updateAABB(); this.map = map; this.objective = new Vector2(0, 0); this.damage = 1; this.speed = 150; this.hp = 100; this.lastDamaged = 0; float x, y; Random ran = new Random(); do { x = ran.nextInt(map.getJ() - 2) * 60; y = ran.nextInt(map.getI() - 2) * -60; } while (map.getTileOf(x, y) == null || map.getTileOf(x, y).isObstacle()); this.setX(x); this.setY(y); } @Override public boolean update(float dTime) { this.delta.x = this.delta.y = 0; knock.x = 0; knock.y = 0; if (this.hp <= 0) return false; if (objective != null && chaseObj) { Vector2 dir = new Vector2(objective.x - this.getX(), objective.y - this.getY()); dir.rotate90(-1); this.setRotation(dir.angle()); sin = SinCosTable.getSin((int) this.getRotation()); cos = SinCosTable.getCos((int) this.getRotation()); delta.x = -sin * speed * dTime; delta.y = cos * speed * dTime; this.setX(this.getX() + this.delta.x); this.setY(this.getY() + this.delta.y); } if (this.delta.x != 0 || this.delta.y != 0) updateAnim(); updateAABB(); return true; } public void setObjective(float x, float y) { if (map != null) { this.objective.x = x; this.objective.y = y; } } public void setObjective(Vector2 obj) { if (obj != null) this.setObjective(obj.x, obj.y); else this.objective = obj; } public void setMap(TileMap m) { map = m; } public void damage(int d, int knockBack) { // Every 200 ms the zombie will be pushed back by the damage(it will be moved back in the current angle) float dTime = Gdx.graphics.getDeltaTime(); if ((System.nanoTime() - lastDamaged) / 1000000 > 200) { lastDamaged = System.nanoTime(); knock.x = sin * dTime * knockBack; knock.y = -cos * dTime * knockBack; this.setX(this.getX() + knock.x); this.setY(this.getY() + knock.y); } this.hp -= d; } public int getDamage() { return this.damage; } public boolean alive() { return this.hp > 0; } public float getCos() { return cos; } public float getSin() { return sin; } public void forceStop() { chaseObj = false; } public void forceStart() { chaseObj = true; } }