Added friendly (bouncy) bullet collision
This commit is contained in:
parent
3f3a638d09
commit
d0edc20cad
@ -26,7 +26,7 @@ public class CannonGame extends AgdxGame {
|
||||
|
||||
@Override
|
||||
public void onUpdate(float delta) {
|
||||
if (Gdx.input.isKeyJustPressed(Keys.F1)) settings.debugEnabled.doSwitch();
|
||||
if (Gdx.input.isKeyJustPressed(Keys.F1) || Gdx.input.isKeyJustPressed(Keys.MENU)) settings.debugEnabled.doSwitch();
|
||||
if (Gdx.input.isKeyJustPressed(Keys.F2)) settings.debugVisualEntities.doSwitch();
|
||||
if (Gdx.input.isKeyJustPressed(Keys.F3)) settings.debugVisualMap.doSwitch();
|
||||
if (Gdx.input.isKeyJustPressed(Keys.F4)) settings.debugVisualMenu.doSwitch();
|
||||
|
@ -1,5 +1,8 @@
|
||||
package de.samdev.cannonshooter.entities;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
|
||||
import de.samdev.absgdx.framework.entities.Entity;
|
||||
@ -13,7 +16,7 @@ import de.samdev.cannonshooter.teams.Team;
|
||||
public class CannonBullet extends Entity {
|
||||
private final static float RESIZE_SPEED = 0.002f;
|
||||
private final static float MAX_LIFETIME = 25 * 1000;
|
||||
|
||||
private final static float BULLET_IGNORE_TIME = 50;
|
||||
private final static float SHOOTING_SPEED = 0.004f;
|
||||
|
||||
public Cannon cannon;
|
||||
@ -25,6 +28,8 @@ public class CannonBullet extends Entity {
|
||||
|
||||
public float lifetime = 0;
|
||||
|
||||
public Map<CannonBullet, Float> ignoredBullets = new HashMap<CannonBullet, Float>();
|
||||
|
||||
public CannonBullet(Cannon owner, Team t) {
|
||||
super(Textures.cannon_bullet, 1f, 1f);
|
||||
this.cannon = owner;
|
||||
@ -104,8 +109,9 @@ public class CannonBullet extends Entity {
|
||||
if (colliderBullet.team != this.team) {
|
||||
colliderBullet.alive = false;
|
||||
this.alive = false;
|
||||
colliderBullet.alive = false;
|
||||
} else {
|
||||
//TODO bounce
|
||||
bounce(colliderBullet);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -119,13 +125,38 @@ public class CannonBullet extends Entity {
|
||||
}
|
||||
}
|
||||
|
||||
private void bounce(CannonBullet other) {
|
||||
float ctime = owner.owner.getCurrentGameTimeMillis();
|
||||
|
||||
if (this.isIgnored(other, ctime)) return;
|
||||
|
||||
this.ignoredBullets.put(other, ctime);
|
||||
other.ignoredBullets.put(this, ctime);
|
||||
|
||||
float dx = other.getPositionX() - this.getPositionX();
|
||||
float dy = other.getPositionY() - this.getPositionY();
|
||||
|
||||
float quadDis = dx * dx + dy * dy;
|
||||
|
||||
float v1d = this.speed.x * dx + this.speed.y * dy;
|
||||
float v2d = other.speed.x * dx + other.speed.y * dy;
|
||||
|
||||
float k1Vx = this.speed.x - dx * (v1d - v2d) / quadDis;
|
||||
float k1Vy = this.speed.y - dy * (v1d - v2d) / quadDis;
|
||||
float k2Vx = other.speed.x - dx * (v2d - v1d) / quadDis;
|
||||
float k2Vy = other.speed.y - dy * (v2d - v1d) / quadDis;
|
||||
|
||||
this.speed.set(k1Vx, k1Vy);
|
||||
other.speed.set(k2Vx, k2Vy);
|
||||
}
|
||||
|
||||
private boolean isIgnored(CannonBullet other, float currentTimeMillis) {
|
||||
return this.ignoredBullets.containsKey(other) && currentTimeMillis - this.ignoredBullets.get(other) < BULLET_IGNORE_TIME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPassiveCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
|
||||
if (activeCollider == cannon) return;
|
||||
|
||||
if (alive && !inBarrel) {
|
||||
alive = false;
|
||||
}
|
||||
//
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -10,7 +10,6 @@ import de.samdev.absgdx.framework.entities.colliosiondetection.geometries.Collis
|
||||
import de.samdev.absgdx.framework.layer.GameLayer;
|
||||
import de.samdev.cannonshooter.Textures;
|
||||
import de.samdev.cannonshooter.ZLayers;
|
||||
import de.samdev.cannonshooter.level.StandardLevel;
|
||||
|
||||
public class CannonHearth extends Entity {
|
||||
private static final Color COLOR_HEARTLESS = new Color(0.75f, 0.75f, 0.75f, 1f);
|
||||
|
@ -39,6 +39,7 @@ public class StandardLevel extends GameLayer {
|
||||
setMapScaleResolver(new ShowCompleteMapScaleResolver());
|
||||
|
||||
addEntity(new Cannon(6, 25, team_player));
|
||||
addEntity(new Cannon(36, 25, team_player));
|
||||
addEntity(new Cannon(48, 6, team_computer1));
|
||||
addEntity(new Cannon(60, 34, team_neutral));
|
||||
}
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user