ProjectZombie/src/shootergame/entity/Entity.java

205 lines
4.8 KiB
Java

package shootergame.entity;
import java.util.ArrayList;
import shootergame.Main;
import shootergame.display.Camera;
import shootergame.display.transparent.ITransparentObject;
import shootergame.display.transparent.TransparentObjects;
import shootergame.init.Entities;
import shootergame.tiles.Tile;
import shootergame.util.math.MathHelpers;
import shootergame.util.math.TileState;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
import shootergame.util.math.vec.Vec3d;
import shootergame.world.World;
import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer;
public class Entity implements ITransparentObject
{
public Vec2d pos;
public double angle;
public boolean opaqueTile = true;
public double hitbox = 1;
public boolean isSolid = false;
public Chunk chunk;
private double speed = 1;
private TileState tile_front;
private TileState tile_back;
public Entity(Vec2d pos, double angle)
{
// Add this entity to the list of entities
Entities.entities.add(this);
// Store the specified values
this.angle = angle;
this.pos = pos;
}
public Entity() {
this(new Vec2d(0, 0), 0);
}
public void tick(Chunk chunk, Layer layer) {
speed = 1;
angle = MathHelpers.floor(angle);
Vec2i tpos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
tile_back = layer.getBackTile(tpos);
tile_front = layer.getFrontTile(tpos);
if(this.isSolid)
{
this.addSlowness(tile_back.tile.slowness);
this.addSlowness(tile_front.tile.slowness);
}
}
public void addSlowness(double amount) {
speed *= (1 - amount);
}
public void render(Vec2d pos, Camera camera) {
}
@Override
public void render(Vec2d pos, Camera camera, short meta) {
this.render(pos, camera);
}
public void doRender(Vec2d pos, Camera camera)
{
if(this.opaqueTile) {
TransparentObjects.register(this, camera, pos, (short)0);
}
else {
this.render(pos, camera);
}
}
@Override
public boolean isOpaqueTile() {
return this.opaqueTile;
}
public void moveForward(double speed) {
this.moveTowards(0, speed);
}
public void moveBackward(double speed) {
this.moveTowards(0, -speed);
}
public void moveTowards(double angle, double speed)
{
// Calculate the new position
speed *= this.speed;
Vec2d pos = this.pos.add(MathHelpers.moveTowards2(speed, Math.toRadians(this.angle + angle)));
// Check if the new position is legal
if(this.moveIsLegal(new Vec2d(this.pos.x, pos.y))) this.pos.y = pos.y;
if(this.moveIsLegal(new Vec2d(pos.x, this.pos.y))) this.pos.x = pos.x;
// Activate stepped on tiles if the entity is "solid"
if(this.isSolid) activateSteppedOnTile();
}
public void moveForward() {
this.moveForward(0.1);
}
public void moveBackward() {
this.moveBackward(0.1);
}
public void moveTowards(double angle) {
moveTowards(angle, 0.1);
}
public void kill() {
chunk.killEntity(this);
}
public void activateTile()
{
// Get the tile position and the layer
Layer layer = Main.world.getLayer();
Vec2i tpos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
// Activate both tiles
tile_front.tile.onActivated(chunk, layer, tpos, this, tile_front.meta);
tile_back.tile.onActivated(chunk, layer, tpos, this, tile_back.meta);
}
public void activateSteppedOnTile()
{
// Get the tile position and the layer
Layer layer = Main.world.getLayer();
Vec2i tpos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
// Activate both tiles
tile_front.tile.onWalkedOn(chunk, layer, tpos, this, tile_front.meta);
tile_back.tile.onWalkedOn(chunk, layer, tpos, this, tile_back.meta);
}
public boolean moveIsLegal(Vec2d pos)
{
Vec2i t_pos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
tile_back = Main.world.getLayer().getBackTile(t_pos);
tile_front = Main.world.getLayer().getFrontTile(t_pos);
// Is this entity solid
if(isSolid)
{
// Get the layer
Layer l = Main.world.getLayer();
// Check the tile the player is standing on
Vec2i tpos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
{
// Get the tile
Tile t = tile_back.tile;
// Send false if the tile isn't walkable
if(!t.tileWalkable) {
return false;
}
}
{
// Get the front tile
Tile t = tile_front.tile;
// Send false if the tile isn't walkable
if(!t.tileWalkable) {
return false;
}
// Check the tiles hitbox if the tile is solid
if(t.tileSolid)
{
// Is the entity in the tiles hitbox
if(pos.squareDistance(new Vec2d(tpos.x + 0.5, tpos.y + 0.5)) < t.tileHitbox)
{
// Send back false
return false;
}
}
}
}
// Send back true if everything passes
return true;
}
@Override
public Vec2d getRenderOffset() {
return new Vec2d(0, 0);
}
}