CannonShooter/core/src/de/samdev/cannonshooter/entities/Cannon.java

141 lines
3.6 KiB
Java
Raw Normal View History

2015-09-14 16:40:30 +02:00
package de.samdev.cannonshooter.entities;
2015-09-17 18:25:04 +02:00
import com.badlogic.gdx.Gdx;
2015-09-17 19:07:49 +02:00
import com.badlogic.gdx.Input.Keys;
2015-09-17 18:25:04 +02:00
2015-09-14 16:40:30 +02:00
import de.samdev.absgdx.framework.entities.Entity;
import de.samdev.absgdx.framework.entities.colliosiondetection.CollisionGeometryOwner;
import de.samdev.absgdx.framework.entities.colliosiondetection.geometries.CollisionGeometry;
import de.samdev.absgdx.framework.layer.GameLayer;
import de.samdev.cannonshooter.Textures;
import de.samdev.cannonshooter.ZLayers;
2015-09-20 13:45:33 +02:00
import de.samdev.cannonshooter.level.StandardLevel;
2015-09-17 19:46:38 +02:00
import de.samdev.cannonshooter.teams.Team;
2015-09-14 16:40:30 +02:00
public class Cannon extends Entity {
2015-09-18 18:44:04 +02:00
private static final float HEALTH_REGEN_PER_HIT = 0.2f;
2015-09-20 13:45:33 +02:00
private static final float START_HEALTH_REGEN = 0.000015f;
private static final float END_HEALTH_REGEN = 0.000105f;
2015-09-18 18:44:04 +02:00
2015-09-17 19:46:38 +02:00
public Team team;
private CannonBarrel barrel;
2015-09-17 15:35:24 +02:00
private CannonHearth hearth;
2015-09-20 13:45:33 +02:00
private StandardLevel level;
2015-09-17 21:02:27 +02:00
public float health; // 1 = active | 0 = neutral
2015-09-17 18:25:04 +02:00
2015-09-17 19:46:38 +02:00
public Cannon(float x, float y, Team t) {
super(Textures.cannon_body, 2, 2);
2015-09-14 16:40:30 +02:00
setPosition(x, y);
setZLayer(ZLayers.LAYER_CANNON_BODY);
2015-09-17 19:46:38 +02:00
team = t;
2015-09-17 21:02:27 +02:00
health = (t.isNeutral) ? 0 : 1;
}
@Override
public void onLayerAdd(GameLayer layer) {
2015-09-20 13:45:33 +02:00
level = (StandardLevel) layer;
2015-09-18 18:37:16 +02:00
addFullCollisionCircle();
//#####################################################################
barrel = new CannonBarrel(this);
2015-09-17 15:35:24 +02:00
hearth = new CannonHearth(this);
layer.addEntity(barrel);
2015-09-17 15:35:24 +02:00
layer.addEntity(hearth);
2015-09-14 16:40:30 +02:00
}
2015-09-17 18:25:04 +02:00
@Override
public void beforeUpdate(float delta) {
2015-09-18 18:37:16 +02:00
if (owner.owner.settings.debugEnabled.get()) {
if (isMouseOverEntity() && Gdx.input.isKeyPressed(Keys.DOWN) && ! team.isNeutral) {
2015-09-20 13:45:33 +02:00
addHealth(-0.01f, level.team_neutral);
2015-09-17 19:07:49 +02:00
}
2015-09-18 18:37:16 +02:00
if (isMouseOverEntity() && Gdx.input.isKeyPressed(Keys.UP) && ! team.isNeutral) {
2015-09-20 13:45:33 +02:00
addHealth(+0.01f, level.team_neutral);
2015-09-17 19:07:49 +02:00
}
2015-09-17 18:25:04 +02:00
}
2015-09-20 13:45:33 +02:00
//#######################
2015-09-18 18:37:16 +02:00
if (isMouseOverEntity() && Gdx.input.justTouched()) {
2015-09-17 19:07:49 +02:00
barrel.startDrag();
2015-09-17 18:25:04 +02:00
}
2015-09-20 13:45:33 +02:00
if (! team.isNeutral && health < 1) {
addHealth(getHealthRegen(delta), team);
}
}
public float getHealthRegen(float delta) {
return (START_HEALTH_REGEN + (END_HEALTH_REGEN - START_HEALTH_REGEN) * health) * delta; // exponential, bitches
2015-09-17 18:25:04 +02:00
}
2015-09-17 19:46:38 +02:00
public void setTeam(Team newteam) {
2015-09-17 21:02:27 +02:00
if (team != newteam) {
team = newteam;
barrel.onTeamChanged();
}
2015-09-17 19:46:38 +02:00
}
2015-09-14 16:40:30 +02:00
@Override
public void onActiveCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
2015-09-17 19:07:49 +02:00
//
2015-09-14 16:40:30 +02:00
}
@Override
public void onPassiveCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
2015-09-17 19:07:49 +02:00
//
2015-09-14 16:40:30 +02:00
}
@Override
public void onActiveMovementCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
2015-09-17 19:07:49 +02:00
//
2015-09-14 16:40:30 +02:00
}
@Override
public void onPassiveMovementCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
2015-09-17 19:07:49 +02:00
//
2015-09-14 16:40:30 +02:00
}
@Override
public boolean canCollideWith(CollisionGeometryOwner other) {
2015-09-18 18:37:16 +02:00
return other instanceof CannonBullet;
2015-09-14 16:40:30 +02:00
}
@Override
public boolean canMoveCollideWith(CollisionGeometryOwner other) {
return false;
}
2015-09-18 18:44:04 +02:00
public void onBulletHit(Team hit_team) {
if (hit_team.isNeutral) return;
2015-09-19 22:50:48 +02:00
if (hit_team == team && health < 1) {
2015-09-20 13:45:33 +02:00
addHealth(HEALTH_REGEN_PER_HIT, hit_team);
2015-09-20 14:30:03 +02:00
} else if (hit_team == team && health == 1) {
barrel.addBooster();
2015-09-19 22:50:48 +02:00
} else if (hit_team != team) {
2015-09-20 13:45:33 +02:00
addHealth(-HEALTH_REGEN_PER_HIT, hit_team);
}
}
private void addHealth(float add, Team hit_team) {
health += add;
if (health <= 0) {
setTeam(hit_team);
health = -health;
} else if (health > 1) {
health = 1;
2015-09-18 18:37:16 +02:00
}
}
2015-09-14 16:40:30 +02:00
}