ProjectZombie/src/shootergame/entity/player/EntityPlayer.java

267 lines
5.4 KiB
Java

package shootergame.entity.player;
import shootergame.Main;
import shootergame.cheats.Cheats;
import shootergame.display.Camera;
import shootergame.entity.Entity;
import shootergame.entity.EntityAlive;
import shootergame.entity.EntityBullet;
import shootergame.entity.EntityInventory;
import shootergame.entity.EntityItem;
import shootergame.entity.EntityVertical;
import shootergame.init.Items;
import shootergame.init.Textures;
import shootergame.inventory.Inventory;
import shootergame.util.gl.GlHelpers;
import shootergame.util.gl.texture.TextureReference;
import shootergame.util.math.ItemStack;
import shootergame.util.math.MathHelpers;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer;
public class EntityPlayer extends EntityVertical implements EntityAlive, EntityInventory
{
public boolean MOVE_FORWARD = false;
public boolean MOVE_BACKWARD = false;
public boolean MOVE_LEFT = false;
public boolean MOVE_RIGHT = false;
public boolean GUN = false;
public boolean moving = false;
public double height = 0;
private int bullet_frequency = 0;
private double health_max = 1000;
private double health = health_max;
public boolean dead = false;
public boolean in_animation = false;
private Inventory inventory;
public int inventory_hand = 0;
public int ammo = 1000;
public int defence_level = 0;
public int gun_level = 0;
private static final Vec2d size = new Vec2d(1, 1);
public EntityPlayer() {
super(TextureReference.EMPTY, size);
this.angle = 45;
// Set some settings
hitbox = 0.5;
isSolid = true;
goThroughSolid = false;
crossUnWalkable = false;
emitsLight = true;
inventory = new Inventory(6);
}
@Override
public double getLightLevel() {
return 1;
}
@Override
public void tick(Chunk chunk, Layer layer)
{
// Handle player deaths
if(health <= 0)
{
if(Cheats.god_mode) {
this.resetHealth();
}
else {
dead = true;
health = 0;
}
}
// Is the player dead or in an animation
if(dead || in_animation) return;
// Call super
super.tick(chunk, layer);
// Rotate left
if(MOVE_LEFT) {
this.angle -= 1;
this.angle = MathHelpers.mod(this.angle, 360);
}
// Rotate right
if(MOVE_RIGHT) {
this.angle += 1;
this.angle = MathHelpers.mod(this.angle, 360);
}
// Move forward
if(MOVE_FORWARD) {
this.moveForward();
}
// Move backward
if(MOVE_BACKWARD) {
this.moveBackward();
}
// Use the gun
if(GUN) {
this.fireBullet(0);
}
}
@Override
public void kill()
{
// Is god mode inactive; kill the player
if(!Cheats.god_mode) {
dead = true;
}
}
@Override
public void moveTowards(double angle, double speed) {
if(dead || in_animation) return;
super.moveTowards(angle, speed);
}
@Override
public void moveForward() {
this.moveForward(0.08);
}
@Override
public void moveBackward(double speed) {
super.moveBackward(0.08);
}
@Override
public void render(Vec2d pos, Camera camera)
{
// Don't render if the player is dead
if(dead) return;
// Translation
GlHelpers.pushMatrix();
GlHelpers.translate(0, 0, height);
// Set the colour due to the lighting
double light = chunk.getLightLevel(new Vec2i(
MathHelpers.floor(pos.x), MathHelpers.floor(pos.y)));
GlHelpers.color3(light, light, light);
// Moving
if(MOVE_BACKWARD || MOVE_FORWARD || moving)
super.render(pos, camera, Textures.ENTITY_PLAYER_MOVING, size);
// Standing still
else super.render(pos, camera, Textures.ENTITY_PLAYER_STILL, size);
// Pop the matrix
GlHelpers.popMatrix();
}
public void fireBullet(double angle)
{
if(dead || in_animation) return;
bullet_frequency += 1;
bullet_frequency %= 10;
// Is there enough ammo and are the bullets at the right frequency
if(bullet_frequency == 0 && ammo > 0)
{
// Remove some ammo
ammo -= 1;
// Summon bullets at this angle relative to the player
int d = (int)(1 + gun_level / 4.0);
int b = (int)(1 + gun_level / 4.0);
Main.world.getLayer().spawnEntity(new EntityBullet(pos.copy(), this, angle + this.angle,
20*d*d, 5/b));
}
}
public void activateItem() {
if(dead || in_animation) return;
ItemStack is = inventory.getItem(inventory_hand);
if(!is.isEmpty()) {
is.item.onAction(is, Main.world.getLayer(), chunk, this);
}
}
@Override
public void addHealth(double amount) {
health += amount;
if(health > health_max) health = health_max;
}
@Override
public void removeHealth(double amount) {
amount = amount / (defence_level / 2.5 + 1);
health -= amount;
}
@Override
public double getHealth() {
return health;
}
@Override
public void resetHealth() {
health = health_max;
}
@Override
public void clearHealth() {
health = 0;
}
@Override
public double maxHealth() {
return health_max;
}
@Override
public void setHealth(double health) {
this.health = health;
}
@Override
public Inventory getInventory() {
return inventory;
}
public void dropItem()
{
ItemStack i = inventory.getItem(inventory_hand);
if(!i.isEmpty())
{
Entity e = new EntityItem(pos.copy(), new ItemStack(i.item, 1, i.meta));
e.angle = angle;
Main.world.getLayer().spawnEntity(e);
i.count -= 1;
}
}
@Override
public void push(double amount, double angle) {
}
@Override
public int bloodParticles() {
return 5;
}
}