ProjectZombie/src/shootergame/entity/Entity.java

151 lines
3.4 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.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
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;
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) {
}
@Override
public void render(Vec2d pos, Camera camera) {
}
public void doRender(Vec2d pos, Camera camera)
{
if(this.opaqueTile) {
TransparentObjects.register(this, camera, pos);
}
else {
this.render(pos, camera);
}
}
@Override
public boolean isOpaqueTile() {
return this.opaqueTile;
}
public void moveForward(double speed) {
Vec2d pos = this.pos.add(MathHelpers.moveTowards2(speed, Math.toRadians(this.angle)));
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;
}
public void moveBackward(double speed) {
Vec2d pos = this.pos.add(MathHelpers.moveTowards2(-speed, Math.toRadians(this.angle)));
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;
}
public void moveForward() {
this.moveForward(0.1);
}
public void moveBackward() {
this.moveBackward(0.1);
}
public void kill() {
chunk.killEntity(this);
}
public boolean moveIsLegal(Vec2d pos)
{
/*// Is this entity solid
if(isSolid)
{
// Get the layer
Layer l = Main.world.getLayer();
// Get some tiles
Vec2i[] tiles = {
new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0),
new Vec2i(MathHelpers.floor(pos.x)+1, MathHelpers.floor(pos.y)+0),
new Vec2i(MathHelpers.floor(pos.x)+1, MathHelpers.floor(pos.y)+1),
new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+1)
};
for(Vec2i tpos : tiles)
{
{
// Get the tile
Tile t = l.getBackTile(tpos);
// Send false if the tile isn't walkable
if(!t.tileWalkable) {
System.out.println("1");
return false;
}
}
{
// Get the front tile
Tile t = l.getFrontTile(tpos);
// Send false if the tile isn't walkable
if(!t.tileWalkable) {
System.out.println("2");
return false;
}
// Check the tiles hitbox if the tile is solid
if(t.tileSolid)
{
// Is the entity in the tiles hitbox
if(
tpos.x + t.tileHitbox < pos.x ||
tpos.x - t.tileHitbox > pos.x ||
tpos.y + t.tileHitbox < pos.y ||
tpos.y - t.tileHitbox > pos.y
) {
System.out.println("3");
// Send back false
return false;
}
}
}
}
}*/
// Send back true if everything passes
return true;
}
}