Bullet Collision

This commit is contained in:
Mike Schwörer 2015-09-18 18:37:16 +02:00
parent a181a786f3
commit c38c0fb099
8 changed files with 135 additions and 35 deletions

View File

@ -64,6 +64,10 @@ project(":core") {
dependencies {
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile name: 'absGDX-framework-1.0'
compile 'org.apache.commons:commons-lang3:3.3.2'
compile 'commons-codec:commons-codec:1.9'
compile 'commons-io:commons-io:2.4'
}
}

View File

@ -5,6 +5,7 @@ import com.badlogic.gdx.Input.Keys;
import de.samdev.absgdx.framework.entities.Entity;
import de.samdev.absgdx.framework.entities.colliosiondetection.CollisionGeometryOwner;
import de.samdev.absgdx.framework.entities.colliosiondetection.geometries.CollisionCircle;
import de.samdev.absgdx.framework.entities.colliosiondetection.geometries.CollisionGeometry;
import de.samdev.absgdx.framework.layer.GameLayer;
import de.samdev.cannonshooter.Textures;
@ -31,6 +32,10 @@ public class Cannon extends Entity {
@Override
public void onLayerAdd(GameLayer layer) {
addFullCollisionCircle();
//#####################################################################
barrel = new CannonBarrel(this);
hearth = new CannonHearth(this);
@ -40,21 +45,17 @@ public class Cannon extends Entity {
@Override
public void beforeUpdate(float delta) {
if (owner.owner.settings.debugEnabled.get())
{
if (isMouseOverEntity() && Gdx.input.isKeyPressed(Keys.DOWN) && ! team.isNeutral)
{
if (owner.owner.settings.debugEnabled.get()) {
if (isMouseOverEntity() && Gdx.input.isKeyPressed(Keys.DOWN) && ! team.isNeutral) {
health = Math.max(0, health - 0.01f);
}
if (isMouseOverEntity() && Gdx.input.isKeyPressed(Keys.UP) && ! team.isNeutral)
{
if (isMouseOverEntity() && Gdx.input.isKeyPressed(Keys.UP) && ! team.isNeutral) {
health = Math.min(1, health + 0.01f);
}
}
if (isMouseOverEntity() && Gdx.input.justTouched())
{
if (isMouseOverEntity() && Gdx.input.justTouched()) {
barrel.startDrag();
}
}
@ -89,7 +90,7 @@ public class Cannon extends Entity {
@Override
public boolean canCollideWith(CollisionGeometryOwner other) {
return false;
return other instanceof CannonBullet;
}
@Override
@ -97,4 +98,11 @@ public class Cannon extends Entity {
return false;
}
public void onBulletHit(CannonBullet bullet, Team hit_team) {
if (hit_team == team) {
health = Math.min(1, health + 0.01f);
} else {
health = Math.max(0, health - 0.01f);
}
}
}

View File

@ -1,11 +1,13 @@
package de.samdev.cannonshooter.entities;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import de.samdev.absgdx.framework.entities.Entity;
import de.samdev.absgdx.framework.entities.colliosiondetection.CollisionGeometryOwner;
import de.samdev.absgdx.framework.entities.colliosiondetection.geometries.CollisionBox;
import de.samdev.absgdx.framework.entities.colliosiondetection.geometries.CollisionGeometry;
import de.samdev.absgdx.framework.layer.GameLayer;
import de.samdev.cannonshooter.Textures;
@ -16,6 +18,7 @@ public class CannonBarrel extends Entity {
private static final float CHARGE_SPEED = 0.00066f;
private static final float UNCHARGE_SPEED = 0.001f;
private static final float ROTATION_SPEED = 0.18f;
private static final float RECOIL_PERC = 0.035f;
private boolean dragging = false;
@ -39,7 +42,11 @@ public class CannonBarrel extends Entity {
@Override
public TextureRegion getTexture() {
return Textures.cannon_barrel[31 - Math.min(31, (int)(charge * 32))];
float realCharge = (charge) / (1 - RECOIL_PERC);
if (charge > (1-RECOIL_PERC)) realCharge = (1 - charge) / RECOIL_PERC;
return Textures.cannon_barrel[31 - Math.min(31, (int)(realCharge * 32))];
}
@Override
@ -100,11 +107,6 @@ public class CannonBarrel extends Entity {
rotation = (rotation + 360) % 360;
}
}
@Override
public float getTextureRotation() {
return rotation;
}
private void updateDragging() {
if (! Gdx.input.isTouched()) {
@ -117,6 +119,11 @@ public class CannonBarrel extends Entity {
targetRotation = mouse.angle();
}
@Override
public float getTextureRotation() {
return rotation;
}
public void startDrag() {
dragging = true;
}
@ -127,10 +134,22 @@ public class CannonBarrel extends Entity {
bullet = null;
loaded = false;
}
@Override
protected void renderTexture(SpriteBatch sbatch, TextureRegion tex, float offsetX, float offsetY) {
// Cause of the different origin
sbatch.draw(
tex,
getPositionX() + offsetX, getPositionY() + offsetY,
getWidth()/4f, getHeight()/2f,
getWidth(), getHeight(),
getTextureScaleX(), getTextureScaleY(),
getTextureRotation());
}
@Override
public void onLayerAdd(GameLayer layer) {
//
//
}
@Override

View File

@ -1,6 +1,5 @@
package de.samdev.cannonshooter.entities;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Rectangle;
import de.samdev.absgdx.framework.entities.Entity;
@ -13,14 +12,19 @@ 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 SHOOTING_SPEED = 0.002f;
private Cannon cannon;
public Cannon cannon;
private final Team team;
private boolean inBarrel = true;
public float scale = 0.001f;
private boolean death = false;
public float lifetime = 0;
public CannonBullet(Cannon owner, Team t) {
super(Textures.cannon_bullet, 0.5f, 0.5f);
this.cannon = owner;
@ -32,6 +36,11 @@ public class CannonBullet extends Entity {
setColorTint(team.teamColor);
}
@Override
public void onLayerAdd(GameLayer layer) {
addFullCollisionCircle();
}
@Override
public void beforeUpdate(float delta) {
@ -44,10 +53,14 @@ public class CannonBullet extends Entity {
}
}
// TODO OPTIMIZE
// TODO Add max lifetime (=> kill() )
if (! owner.getVisibleMapBox().overlaps(new Rectangle(getPositionX(), getPositionY(), getWidth(), getHeight())))
{
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()) {
alive = false;
}
}
@ -55,7 +68,7 @@ public class CannonBullet extends Entity {
public void shoot(float rotation) {
inBarrel = false;
speed.set(0.001f, 0);
speed.set(SHOOTING_SPEED, 0);
speed.rotate(rotation);
}
@ -72,20 +85,47 @@ public class CannonBullet extends Entity {
public float getTextureScaleY() {
return scale;
}
@Override
public void onLayerAdd(GameLayer layer) {
//
}
@Override
public void onActiveCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
//
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;
}
}
@Override
public void onPassiveCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
//
if (activeCollider == cannon) return;
if (alive && !inBarrel) {
alive = false;
}
}
@Override
@ -100,7 +140,7 @@ public class CannonBullet extends Entity {
@Override
public boolean canCollideWith(CollisionGeometryOwner other) {
return false;
return other instanceof CannonBullet || other instanceof Cannon;
}
@Override

View File

@ -13,6 +13,7 @@ import de.samdev.absgdx.framework.map.mapscaleresolver.ShowCompleteMapScaleResol
import de.samdev.cannonshooter.Textures;
import de.samdev.cannonshooter.entities.Cannon;
import de.samdev.cannonshooter.teams.Team;
import de.samdev.cannonshooter.tiles.StandardTile;
public class StandardLevel extends GameLayer {
@ -26,7 +27,7 @@ public class StandardLevel extends GameLayer {
private Team team_computer3 = new Team(12, Team.COL_P4, false, true, false, Team.MULTIPLIER_AI_D0);
public StandardLevel(AgdxGame owner) {
super(owner, TileMap.createEmptyMap(32, 20));
super(owner, TileMap.createEmptyMapUnsafe(35, 20, StandardTile.class));
initTeams();
@ -37,9 +38,9 @@ public class StandardLevel extends GameLayer {
addBackground(new RepeatingBackground(Textures.texbackground, 1/32f));
setMapScaleResolver(new ShowCompleteMapScaleResolver());
addEntity(new Cannon(7, 13, team_player));
addEntity(new Cannon(14, 5, team_computer1));
addEntity(new Cannon(20, 13, team_neutral));
addEntity(new Cannon(3, 13, team_player));
addEntity(new Cannon(24, 3, team_computer1));
addEntity(new Cannon(30, 17, team_neutral));
}
private void initTeams() {

View File

@ -0,0 +1,28 @@
package de.samdev.cannonshooter.tiles;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import de.samdev.absgdx.framework.entities.colliosiondetection.CollisionGeometryOwner;
import de.samdev.absgdx.framework.map.Tile;
public class StandardTile extends Tile {
public StandardTile() {
super((TextureRegion)null);
}
@Override
public boolean canMoveCollideWith(CollisionGeometryOwner other) {
return false;
}
@Override
public boolean canCollideWith(CollisionGeometryOwner other) {
return false;
}
@Override
public void update(float delta) { /* */ }
}

Binary file not shown.

Binary file not shown.