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

152 lines
3.5 KiB
Java
Raw Normal View History

2015-09-17 19:07:49 +02:00
package de.samdev.cannonshooter.entities;
2015-09-17 21:02:27 +02:00
import com.badlogic.gdx.math.Rectangle;
2015-09-17 19:07:49 +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-17 21:02:27 +02:00
import de.samdev.cannonshooter.teams.Team;
2015-09-17 19:07:49 +02:00
public class CannonBullet extends Entity {
2015-09-18 15:04:14 +02:00
private final static float RESIZE_SPEED = 0.002f;
2015-09-18 18:37:16 +02:00
private final static float MAX_LIFETIME = 25 * 1000;
private final static float SHOOTING_SPEED = 0.002f;
2015-09-17 21:02:27 +02:00
2015-09-18 18:37:16 +02:00
public Cannon cannon;
2015-09-17 21:02:27 +02:00
private final Team team;
private boolean inBarrel = true;
2015-09-18 15:04:14 +02:00
public float scale = 0.001f;
2015-09-17 21:02:27 +02:00
private boolean death = false;
2015-09-17 19:07:49 +02:00
2015-09-18 18:37:16 +02:00
public float lifetime = 0;
2015-09-17 21:02:27 +02:00
public CannonBullet(Cannon owner, Team t) {
2015-09-18 15:04:14 +02:00
super(Textures.cannon_bullet, 0.5f, 0.5f);
2015-09-17 21:02:27 +02:00
this.cannon = owner;
this.team = t;
2015-09-17 19:07:49 +02:00
setPosition(cannon.getPositionX(), cannon.getPositionY());
setZLayer(ZLayers.LAYER_CANNON_BULLET);
2015-09-17 21:02:27 +02:00
setColorTint(team.teamColor);
2015-09-17 19:07:49 +02:00
}
2015-09-18 18:37:16 +02:00
@Override
public void onLayerAdd(GameLayer layer) {
addFullCollisionCircle();
}
2015-09-17 19:07:49 +02:00
2015-09-17 21:02:27 +02:00
@Override
public void beforeUpdate(float delta) {
if (death) {
scale -= RESIZE_SPEED * delta;
if (scale <= 0) {
death = false;
scale = 0.0001f;
alive = false;
}
}
2015-09-18 18:37:16 +02:00
lifetime += delta;
if (lifetime > MAX_LIFETIME) {
kill();
}
Rectangle vbox = owner.getVisibleMapBox();
if (vbox.x > getPositionRightX() || vbox.y > getPositionTopY() || vbox.x+vbox.width < getPositionX() || vbox.y+vbox.height < getPositionY()) {
2015-09-17 21:02:27 +02:00
alive = false;
}
}
public void shoot(float rotation) {
inBarrel = false;
2015-09-18 18:37:16 +02:00
speed.set(SHOOTING_SPEED, 0);
2015-09-17 21:02:27 +02:00
speed.rotate(rotation);
}
public void kill() {
death = true;
}
@Override
public float getTextureScaleX() {
return scale;
}
@Override
public float getTextureScaleY() {
return scale;
}
2015-09-17 19:07:49 +02:00
@Override
public void onActiveCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
2015-09-18 18:37:16 +02:00
if (passiveCollider == cannon) return;
if (! alive) return;
if (inBarrel) return;
if (passiveCollider instanceof CannonBullet) {
CannonBullet colliderBullet = (CannonBullet)passiveCollider;
if (colliderBullet.inBarrel) {
colliderBullet.cannon.onBulletHit(this, this.team);
alive = false;
} else {
if (colliderBullet.team != this.team) {
colliderBullet.alive = false;
this.alive = false;
} else {
//TODO bounce
}
}
}
if (passiveCollider instanceof Cannon) {
Cannon cannon = (Cannon)passiveCollider;
cannon.onBulletHit(this, this.team);
alive = false;
}
2015-09-17 19:07:49 +02:00
}
@Override
public void onPassiveCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
2015-09-18 18:37:16 +02:00
if (activeCollider == cannon) return;
if (alive && !inBarrel) {
alive = false;
}
2015-09-17 19:07:49 +02:00
}
@Override
public void onActiveMovementCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
//
}
@Override
public void onPassiveMovementCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
//
}
@Override
public boolean canCollideWith(CollisionGeometryOwner other) {
2015-09-18 18:37:16 +02:00
return other instanceof CannonBullet || other instanceof Cannon;
2015-09-17 19:07:49 +02:00
}
@Override
public boolean canMoveCollideWith(CollisionGeometryOwner other) {
return false;
}
}