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 com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Vector2; import com.octa.topdown.armory.Weapon; import com.octa.topdown.system.InputTracker; import com.octa.topdown.system.ResourceHandler; import com.octa.topdown.system.SinCosTable; public class Player extends Entity { private int hp; private int ammo; private Weapon arm; private Weapon stick; private Weapon gun; private int score; private long lastDamaged; private long damageDelay; public Player(int animId, float x, float y, int width, int height) { super(animId, x, y, width, height); this.bbDif = 4; this.hp = 100; this.ammo = 400; gun = new Weapon(ResourceHandler.createAnimation("Soldier_atlas.png", 1, 0, 60, 60, 60), 130, 10, 1000, 1000, 2000, false); stick = new Weapon(ResourceHandler.createAnimation("Soldier_atlas.png", 3, 0, 118, 60, 60), 30, 3, 50, 0, 2000, true); stick.setAABB(AABB); arm = stick; this.speed = 270; score = 0; lastDamaged = 0; damageDelay = 600; knock = new Vector2(); updateAABB(); } @Override public boolean update(float dTime) { knock.x = 0; knock.y = 0; this.delta.x = 0; this.delta.y = 0; // Handle user input if (InputTracker.isPressed(InputTracker.DOWN)) { this.delta.y = -this.speed * dTime; } if (InputTracker.isPressed(InputTracker.UP)) { this.delta.y = this.speed * dTime; } if (InputTracker.isPressed(InputTracker.RIGHT)) { this.delta.x = this.speed * dTime; } if (InputTracker.isPressed(InputTracker.LEFT)) { this.delta.x = -this.speed * dTime; } if (InputTracker.isJustReleased(InputTracker.UP) || InputTracker.isJustReleased(InputTracker.DOWN)) { this.delta.y = 0; } if (InputTracker.isJustReleased(InputTracker.LEFT) || InputTracker.isJustReleased(InputTracker.RIGHT)) { this.delta.x = 0; } if (InputTracker.isJustReleased(InputTracker.SPACE)) { arm = arm == gun ? stick : gun; } // Animate sprite if moving if (this.delta.x != 0 || this.delta.y != 0) { updateAnim(); } // Cast a vector from the player's position in the screen(always the center) // to the mouse pointer. Then, get that vector's angle and set it to the sprite's // rotation, so the sprite "faces" the mouse. float midX = Gdx.graphics.getWidth() / 2; float midY = Gdx.graphics.getHeight() / 2; float mouseX = InputTracker.getMousePos().x; float mouseY = Gdx.graphics.getHeight() - InputTracker.getMousePos().y; Vector2 dir = new Vector2(mouseX - midX, mouseY - midY); dir.rotate90(-1); this.setRotation(dir.angle()); // Update player's position this.setX(this.getX() + this.delta.x); this.setY(this.getY() + this.delta.y); if (Gdx.input.isTouched()) { float px = this.getX() + (this.getWidth() / 2); float py = this.getY() + (this.getHeight() / 2); px -= SinCosTable.getSin((int) this.getRotation() + 20) * 23; py += SinCosTable.getCos((int) this.getRotation() + 20) * 23; if (!arm.isMeele()) { if (ammo > 0) { ammo--; arm.shoot(px, py, getRotation()); } } else { arm.shoot(px, py, getRotation()); } } updateAABB(); stick.setAABB(AABB); return true; } @Override public void draw(SpriteBatch batch) { super.draw(batch); arm.draw(batch, getX(), getY(), this.getRotation()); } // Just boring getters and setters public int getAmmo() { return this.ammo; } public void addAmmo(int amount) { this.ammo += amount; } public int getHp() { return this.hp; } public void damage(int amount, float xDir, float yDir) { if ((System.nanoTime() - lastDamaged) / 1000000 > damageDelay) { lastDamaged = System.nanoTime(); float dTime = Gdx.graphics.getDeltaTime(); knock.x = xDir * 2000 * dTime; knock.y = yDir * 2000 * dTime; this.setX(this.getX() + knock.x); this.setY(this.getY() + knock.y); this.hp -= amount; } } public boolean alive() { return this.hp > 0; } public void addScore(int a) { this.score += a; } public int getScore() { return score; } public void setWeapon(Weapon w) { this.arm = w; } public Weapon getWeapon() { return this.arm; } public void heal(int am) { this.hp += am; } public boolean isDamaged() { return ((System.nanoTime() - lastDamaged) / 1000000 < damageDelay) ? true : false; } @Override public void dispose() { super.dispose(); } }