Bullet Collision
This commit is contained in:
parent
a181a786f3
commit
c38c0fb099
@ -64,6 +64,10 @@ project(":core") {
|
|||||||
dependencies {
|
dependencies {
|
||||||
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
|
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
|
||||||
compile name: 'absGDX-framework-1.0'
|
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'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import com.badlogic.gdx.Input.Keys;
|
|||||||
|
|
||||||
import de.samdev.absgdx.framework.entities.Entity;
|
import de.samdev.absgdx.framework.entities.Entity;
|
||||||
import de.samdev.absgdx.framework.entities.colliosiondetection.CollisionGeometryOwner;
|
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.entities.colliosiondetection.geometries.CollisionGeometry;
|
||||||
import de.samdev.absgdx.framework.layer.GameLayer;
|
import de.samdev.absgdx.framework.layer.GameLayer;
|
||||||
import de.samdev.cannonshooter.Textures;
|
import de.samdev.cannonshooter.Textures;
|
||||||
@ -31,6 +32,10 @@ public class Cannon extends Entity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLayerAdd(GameLayer layer) {
|
public void onLayerAdd(GameLayer layer) {
|
||||||
|
addFullCollisionCircle();
|
||||||
|
|
||||||
|
//#####################################################################
|
||||||
|
|
||||||
barrel = new CannonBarrel(this);
|
barrel = new CannonBarrel(this);
|
||||||
hearth = new CannonHearth(this);
|
hearth = new CannonHearth(this);
|
||||||
|
|
||||||
@ -40,21 +45,17 @@ public class Cannon extends Entity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeUpdate(float delta) {
|
public void beforeUpdate(float delta) {
|
||||||
if (owner.owner.settings.debugEnabled.get())
|
if (owner.owner.settings.debugEnabled.get()) {
|
||||||
{
|
if (isMouseOverEntity() && Gdx.input.isKeyPressed(Keys.DOWN) && ! team.isNeutral) {
|
||||||
if (isMouseOverEntity() && Gdx.input.isKeyPressed(Keys.DOWN) && ! team.isNeutral)
|
|
||||||
{
|
|
||||||
health = Math.max(0, health - 0.01f);
|
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);
|
health = Math.min(1, health + 0.01f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isMouseOverEntity() && Gdx.input.justTouched())
|
if (isMouseOverEntity() && Gdx.input.justTouched()) {
|
||||||
{
|
|
||||||
barrel.startDrag();
|
barrel.startDrag();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,7 +90,7 @@ public class Cannon extends Entity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canCollideWith(CollisionGeometryOwner other) {
|
public boolean canCollideWith(CollisionGeometryOwner other) {
|
||||||
return false;
|
return other instanceof CannonBullet;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -97,4 +98,11 @@ public class Cannon extends Entity {
|
|||||||
return false;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
package de.samdev.cannonshooter.entities;
|
package de.samdev.cannonshooter.entities;
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
|
|
||||||
import de.samdev.absgdx.framework.entities.Entity;
|
import de.samdev.absgdx.framework.entities.Entity;
|
||||||
import de.samdev.absgdx.framework.entities.colliosiondetection.CollisionGeometryOwner;
|
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.entities.colliosiondetection.geometries.CollisionGeometry;
|
||||||
import de.samdev.absgdx.framework.layer.GameLayer;
|
import de.samdev.absgdx.framework.layer.GameLayer;
|
||||||
import de.samdev.cannonshooter.Textures;
|
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 CHARGE_SPEED = 0.00066f;
|
||||||
private static final float UNCHARGE_SPEED = 0.001f;
|
private static final float UNCHARGE_SPEED = 0.001f;
|
||||||
private static final float ROTATION_SPEED = 0.18f;
|
private static final float ROTATION_SPEED = 0.18f;
|
||||||
|
private static final float RECOIL_PERC = 0.035f;
|
||||||
|
|
||||||
private boolean dragging = false;
|
private boolean dragging = false;
|
||||||
|
|
||||||
@ -39,7 +42,11 @@ public class CannonBarrel extends Entity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TextureRegion getTexture() {
|
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
|
@Override
|
||||||
@ -101,11 +108,6 @@ public class CannonBarrel extends Entity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public float getTextureRotation() {
|
|
||||||
return rotation;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateDragging() {
|
private void updateDragging() {
|
||||||
if (! Gdx.input.isTouched()) {
|
if (! Gdx.input.isTouched()) {
|
||||||
dragging = false;
|
dragging = false;
|
||||||
@ -117,6 +119,11 @@ public class CannonBarrel extends Entity {
|
|||||||
targetRotation = mouse.angle();
|
targetRotation = mouse.angle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getTextureRotation() {
|
||||||
|
return rotation;
|
||||||
|
}
|
||||||
|
|
||||||
public void startDrag() {
|
public void startDrag() {
|
||||||
dragging = true;
|
dragging = true;
|
||||||
}
|
}
|
||||||
@ -128,6 +135,18 @@ public class CannonBarrel extends Entity {
|
|||||||
loaded = false;
|
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
|
@Override
|
||||||
public void onLayerAdd(GameLayer layer) {
|
public void onLayerAdd(GameLayer layer) {
|
||||||
//
|
//
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package de.samdev.cannonshooter.entities;
|
package de.samdev.cannonshooter.entities;
|
||||||
|
|
||||||
import com.badlogic.gdx.graphics.Color;
|
|
||||||
import com.badlogic.gdx.math.Rectangle;
|
import com.badlogic.gdx.math.Rectangle;
|
||||||
|
|
||||||
import de.samdev.absgdx.framework.entities.Entity;
|
import de.samdev.absgdx.framework.entities.Entity;
|
||||||
@ -13,14 +12,19 @@ import de.samdev.cannonshooter.teams.Team;
|
|||||||
|
|
||||||
public class CannonBullet extends Entity {
|
public class CannonBullet extends Entity {
|
||||||
private final static float RESIZE_SPEED = 0.002f;
|
private final static float RESIZE_SPEED = 0.002f;
|
||||||
|
private final static float MAX_LIFETIME = 25 * 1000;
|
||||||
|
|
||||||
private Cannon cannon;
|
private final static float SHOOTING_SPEED = 0.002f;
|
||||||
|
|
||||||
|
public Cannon cannon;
|
||||||
private final Team team;
|
private final Team team;
|
||||||
|
|
||||||
private boolean inBarrel = true;
|
private boolean inBarrel = true;
|
||||||
public float scale = 0.001f;
|
public float scale = 0.001f;
|
||||||
private boolean death = false;
|
private boolean death = false;
|
||||||
|
|
||||||
|
public float lifetime = 0;
|
||||||
|
|
||||||
public CannonBullet(Cannon owner, Team t) {
|
public CannonBullet(Cannon owner, Team t) {
|
||||||
super(Textures.cannon_bullet, 0.5f, 0.5f);
|
super(Textures.cannon_bullet, 0.5f, 0.5f);
|
||||||
this.cannon = owner;
|
this.cannon = owner;
|
||||||
@ -33,6 +37,11 @@ public class CannonBullet extends Entity {
|
|||||||
setColorTint(team.teamColor);
|
setColorTint(team.teamColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLayerAdd(GameLayer layer) {
|
||||||
|
addFullCollisionCircle();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeUpdate(float delta) {
|
public void beforeUpdate(float delta) {
|
||||||
if (death) {
|
if (death) {
|
||||||
@ -44,10 +53,14 @@ public class CannonBullet extends Entity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO OPTIMIZE
|
lifetime += delta;
|
||||||
// TODO Add max lifetime (=> kill() )
|
|
||||||
if (! owner.getVisibleMapBox().overlaps(new Rectangle(getPositionX(), getPositionY(), getWidth(), getHeight())))
|
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;
|
alive = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,7 +68,7 @@ public class CannonBullet extends Entity {
|
|||||||
public void shoot(float rotation) {
|
public void shoot(float rotation) {
|
||||||
inBarrel = false;
|
inBarrel = false;
|
||||||
|
|
||||||
speed.set(0.001f, 0);
|
speed.set(SHOOTING_SPEED, 0);
|
||||||
speed.rotate(rotation);
|
speed.rotate(rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,19 +86,46 @@ public class CannonBullet extends Entity {
|
|||||||
return scale;
|
return scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onLayerAdd(GameLayer layer) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onActiveCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
|
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
|
@Override
|
||||||
public void onPassiveCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
|
public void onPassiveCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
|
||||||
//
|
if (activeCollider == cannon) return;
|
||||||
|
|
||||||
|
if (alive && !inBarrel) {
|
||||||
|
alive = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -100,7 +140,7 @@ public class CannonBullet extends Entity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canCollideWith(CollisionGeometryOwner other) {
|
public boolean canCollideWith(CollisionGeometryOwner other) {
|
||||||
return false;
|
return other instanceof CannonBullet || other instanceof Cannon;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -13,6 +13,7 @@ import de.samdev.absgdx.framework.map.mapscaleresolver.ShowCompleteMapScaleResol
|
|||||||
import de.samdev.cannonshooter.Textures;
|
import de.samdev.cannonshooter.Textures;
|
||||||
import de.samdev.cannonshooter.entities.Cannon;
|
import de.samdev.cannonshooter.entities.Cannon;
|
||||||
import de.samdev.cannonshooter.teams.Team;
|
import de.samdev.cannonshooter.teams.Team;
|
||||||
|
import de.samdev.cannonshooter.tiles.StandardTile;
|
||||||
|
|
||||||
public class StandardLevel extends GameLayer {
|
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);
|
private Team team_computer3 = new Team(12, Team.COL_P4, false, true, false, Team.MULTIPLIER_AI_D0);
|
||||||
|
|
||||||
public StandardLevel(AgdxGame owner) {
|
public StandardLevel(AgdxGame owner) {
|
||||||
super(owner, TileMap.createEmptyMap(32, 20));
|
super(owner, TileMap.createEmptyMapUnsafe(35, 20, StandardTile.class));
|
||||||
|
|
||||||
initTeams();
|
initTeams();
|
||||||
|
|
||||||
@ -37,9 +38,9 @@ public class StandardLevel extends GameLayer {
|
|||||||
addBackground(new RepeatingBackground(Textures.texbackground, 1/32f));
|
addBackground(new RepeatingBackground(Textures.texbackground, 1/32f));
|
||||||
setMapScaleResolver(new ShowCompleteMapScaleResolver());
|
setMapScaleResolver(new ShowCompleteMapScaleResolver());
|
||||||
|
|
||||||
addEntity(new Cannon(7, 13, team_player));
|
addEntity(new Cannon(3, 13, team_player));
|
||||||
addEntity(new Cannon(14, 5, team_computer1));
|
addEntity(new Cannon(24, 3, team_computer1));
|
||||||
addEntity(new Cannon(20, 13, team_neutral));
|
addEntity(new Cannon(30, 17, team_neutral));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initTeams() {
|
private void initTeams() {
|
||||||
|
28
core/src/de/samdev/cannonshooter/tiles/StandardTile.java
Normal file
28
core/src/de/samdev/cannonshooter/tiles/StandardTile.java
Normal 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.
Loading…
Reference in New Issue
Block a user