From efcd34ebffc3d045e389d028b04c03d56af7aa83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Thu, 17 Sep 2015 19:07:49 +0200 Subject: [PATCH] Added Rotate Gesture to Barrel --- android/assets/cannon_bullet.png | Bin 0 -> 1255 bytes .../src/de/samdev/cannonshooter/Textures.java | 1 + .../samdev/cannonshooter/entities/Cannon.java | 30 ++++--- .../cannonshooter/entities/CannonBarrel.java | 84 ++++++++++++------ .../cannonshooter/entities/CannonBullet.java | 66 ++++++++++++++ .../cannonshooter/entities/CannonHearth.java | 65 +++++++------- .../framework/TileAlignedBackground.java | 61 ------------- .../cannonshooter/level/StandardLevel.java | 2 - .../samdev/cannonshooter/util/MathUtils.java | 16 ++++ 9 files changed, 185 insertions(+), 140 deletions(-) create mode 100644 android/assets/cannon_bullet.png create mode 100644 core/src/de/samdev/cannonshooter/entities/CannonBullet.java delete mode 100644 core/src/de/samdev/cannonshooter/framework/TileAlignedBackground.java create mode 100644 core/src/de/samdev/cannonshooter/util/MathUtils.java diff --git a/android/assets/cannon_bullet.png b/android/assets/cannon_bullet.png new file mode 100644 index 0000000000000000000000000000000000000000..93b429694628db9646939289bb14d21948b7d918 GIT binary patch literal 1255 zcmVPx#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D02p*dSaefwW^{L9 za%BK;VQFr3E^cLXAT%y8E;js(W8VM(1XxK#K~!i%)!7Gg+&T;dVE_N0=-zwpUGF|+ z_8!IX;%>{F*-S){-~w2Hx@4~Fx=+L6;$pY7wA3vxFLx^|D@}M;@B0XvlW#!?udc3k zYin!W#>Pgsxw+Y6Yinx&bKZls_SE*?iss}?SXfx-BaOiN`uddY_V#wSv$NCf?(TMb zdwV_h_xF3`**j~jjZW)k`n;FWoNR%h6MJbRaQ_Snk?TWlsH_4%{V zoUD@kby52W@p`cF@bJ)QgtVL? zP|keGiRPq&ZIP#0k&^z;AYKnP*%lyYIrAka{+iI^6)gR=$cxlhkk5z{&WaP{G^-{C z58zf!w~6LU@(S{+hclez%*Y8BoF-$yckob3M<-BB(foIj&nV@T!q&cPDNX+}KR@4J zRFRH$W}~9|D$Tz%oT*vhfD1X3Qh7&A9jK!p>F5kUK|VvL!vPnZa4WMgb)XgfNarU9 z*-}8Ag~|Be0i9=z(a*r4%tBcUy#p>SA5c8wVYL6#(a)%}PsE8>K~H>vt%^Tztp(-(mKa9A|oGsIHnu*?UVj#D*7Ko_h}8B z>N;o^Fb8OEY=Z;2nQ@&u;AZ1M`oCfsqXRgV4&Xe~fiV?uM%jsetpm*r)HU#1B-Gcy zukS*?{vMdTo2zePi^g~p<5b?nlkw+UsQi>3`?nBogKr^!%(~3RQU|Ty1oS@{e;k1N z_rREBghTzSY%+S@QwPTSi_9oLAxinc)PcxIYSN8l;irF 0) + if (owner.owner.settings.debugEnabled.get()) { - power -= 0.1; + if (isMouseOverEntity() && Gdx.input.isKeyPressed(Keys.DOWN)) + { + power = Math.max(0, power - 0.01f); + } + + if (isMouseOverEntity() && Gdx.input.isKeyPressed(Keys.UP)) + { + power = Math.min(1, power + 0.01f); + } } - if (isMouseOverEntity() && Gdx.input.justTouched() && Gdx.input.isButtonPressed(Buttons.RIGHT) && power < 1) + if (isMouseOverEntity() && Gdx.input.justTouched()) { - power += 0.1; + barrel.startDrag(); } } @Override public void onActiveCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { - // TODO Auto-generated method stub - + // } @Override public void onPassiveCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { - // TODO Auto-generated method stub - + // } @Override public void onActiveMovementCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { - // TODO Auto-generated method stub - + // } @Override public void onPassiveMovementCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { - // TODO Auto-generated method stub - + // } @Override diff --git a/core/src/de/samdev/cannonshooter/entities/CannonBarrel.java b/core/src/de/samdev/cannonshooter/entities/CannonBarrel.java index 57468f0..bffa099 100644 --- a/core/src/de/samdev/cannonshooter/entities/CannonBarrel.java +++ b/core/src/de/samdev/cannonshooter/entities/CannonBarrel.java @@ -1,8 +1,7 @@ package de.samdev.cannonshooter.entities; -import com.badlogic.gdx.graphics.Color; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.math.Vector2; import de.samdev.absgdx.framework.entities.Entity; import de.samdev.absgdx.framework.entities.colliosiondetection.CollisionGeometryOwner; @@ -10,42 +9,86 @@ 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.util.MathUtils; public class CannonBarrel extends Entity { - private static final float ANIMATION_DURATION = 1000; + private static final float CHARGE_DURATION = 0.0001f; + private static final float ROTATION_SPEED = 0.18f; + + private boolean dragging = false; private float rotation = 0; + private float targetRotation = 0; + private float charge = 0; + + private Cannon cannon; public CannonBarrel(Cannon owner) { super(Textures.cannon_barrel[0], 4, 2); + cannon = owner; setPosition(owner.getPositionX() - 1, owner.getPositionY()); setZLayer(ZLayers.LAYER_CANNON_BARREL); } + + @Override + public void beforeUpdate(float delta) { + if (dragging) updateDragging(); + + if (rotation != targetRotation) { + float sign = MathUtils.moduloSignum(rotation, targetRotation, 360, 1); + + rotation += sign * ROTATION_SPEED * delta; + if (sign != MathUtils.moduloSignum(rotation, targetRotation, 360, 1)) rotation = targetRotation; + + rotation = (rotation + 360) % 360; + } + } + + @Override + public float getTextureRotation() { + return rotation; + } + + private void updateDragging() { + if (! Gdx.input.isTouched()) { + dragging = false; + return; + } + + Vector2 mouse = new Vector2(owner.getMouseOnMapPositionX() - cannon.getCenterX(), owner.getMouseOnMapPositionY() - cannon.getCenterY()); + + targetRotation = mouse.angle(); + } + + public void startDrag() { + dragging = true; + } + + @Override + public void onLayerAdd(GameLayer layer) { + // + } @Override public void onActiveCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { - // TODO Auto-generated method stub - + // } @Override public void onPassiveCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { - // TODO Auto-generated method stub - + // } @Override public void onActiveMovementCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { - // TODO Auto-generated method stub - + // } @Override public void onPassiveMovementCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { - // TODO Auto-generated method stub - + // } @Override @@ -58,21 +101,4 @@ public class CannonBarrel extends Entity { return false; } - @Override - public void beforeUpdate(float delta) { - rotation += 1; - rotation = (rotation + 1) % 360; - } - - @Override - public float getTextureRotation() { - return 0; - } - - @Override - public void onLayerAdd(GameLayer layer) { - // TODO Auto-generated method stub - - } - } diff --git a/core/src/de/samdev/cannonshooter/entities/CannonBullet.java b/core/src/de/samdev/cannonshooter/entities/CannonBullet.java new file mode 100644 index 0000000..1d35d59 --- /dev/null +++ b/core/src/de/samdev/cannonshooter/entities/CannonBullet.java @@ -0,0 +1,66 @@ +package de.samdev.cannonshooter.entities; + +import com.badlogic.gdx.graphics.Color; + +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; + +public class CannonBullet extends Entity { + private Cannon cannon; + + public CannonBullet(Cannon owner) { + super(Textures.cannon_bullet, 0.25f, 0.25f); + cannon = owner; + + setPosition(cannon.getPositionX(), cannon.getPositionY()); + + setZLayer(ZLayers.LAYER_CANNON_BULLET); + + setColorTint(Color.RED); + } + + @Override + public void onLayerAdd(GameLayer layer) { + // + } + + @Override + public void onActiveCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { + // + } + + @Override + public void onPassiveCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { + // + } + + @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) { + return false; + } + + @Override + public boolean canMoveCollideWith(CollisionGeometryOwner other) { + return false; + } + + @Override + public void beforeUpdate(float delta) { + // + } + +} diff --git a/core/src/de/samdev/cannonshooter/entities/CannonHearth.java b/core/src/de/samdev/cannonshooter/entities/CannonHearth.java index a2b75d5..296d4ee 100644 --- a/core/src/de/samdev/cannonshooter/entities/CannonHearth.java +++ b/core/src/de/samdev/cannonshooter/entities/CannonHearth.java @@ -41,40 +41,6 @@ public class CannonHearth extends Entity { sbatch.setColor(Color.WHITE); } - @Override - public void onActiveCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { - // TODO Auto-generated method stub - - } - - @Override - public void onPassiveCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { - // TODO Auto-generated method stub - - } - - @Override - public void onActiveMovementCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { - // TODO Auto-generated method stub - - } - - @Override - public void onPassiveMovementCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { - // TODO Auto-generated method stub - - } - - @Override - public boolean canCollideWith(CollisionGeometryOwner other) { - return false; - } - - @Override - public boolean canMoveCollideWith(CollisionGeometryOwner other) { - return false; - } - @Override public void beforeUpdate(float delta) { if (cannon.power < 1) @@ -99,8 +65,37 @@ public class CannonHearth extends Entity { @Override public void onLayerAdd(GameLayer layer) { - // TODO Auto-generated method stub + // + } + @Override + public void onActiveCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { + // + } + + @Override + public void onPassiveCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) { + // + } + + @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) { + return false; + } + + @Override + public boolean canMoveCollideWith(CollisionGeometryOwner other) { + return false; } } diff --git a/core/src/de/samdev/cannonshooter/framework/TileAlignedBackground.java b/core/src/de/samdev/cannonshooter/framework/TileAlignedBackground.java deleted file mode 100644 index 8921e7d..0000000 --- a/core/src/de/samdev/cannonshooter/framework/TileAlignedBackground.java +++ /dev/null @@ -1,61 +0,0 @@ -package de.samdev.cannonshooter.framework; - -import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; -import com.badlogic.gdx.graphics.g2d.TextureRegion; -import com.badlogic.gdx.math.Rectangle; -import com.badlogic.gdx.math.Vector2; - -import de.samdev.absgdx.framework.map.TileMap; -import de.samdev.absgdx.framework.map.background.MapBackground; - -/** - * This is a repeating background. - * - * The texture will be repeated from the map-origin (0|0) to cover the whole map - * The texture tiles are aligned to the map tiles, with scale=1 the texture-tiles equal the map-tiles - * But the texture-tiles go on after the map borders and fill the whole viewport - */ -public class TileAlignedBackground extends MapBackground { //TODO Merge back to absGDX - - private final TextureRegion texture; - private final float scale; - - /** - * Create a new repeating Background aligned to the TiledMap - * - * @param tex the background texture - * @param scale size of a background tile (1 = Background equals MapTile) (2 = Background equals 4 MapTile) ... - */ - public TileAlignedBackground(TextureRegion tex, float scale) { - this.texture = tex; - this.scale = scale; - } - - - /** - * Create a new repeating Background aligned to the TiledMap - * - * @param tex the background texture - * @param scale size of a background tile (1 = Background equals MapTile) (2 = Background equals 4 MapTile) ... - */ - public TileAlignedBackground(Texture tex, float scale) { - this(new TextureRegion(tex), scale); - } - - @Override - public void draw(SpriteBatch sbatch, Vector2 map_offset, TileMap map, Rectangle visible) { - int minX = (int)(visible.x + map_offset.x); - int minY = (int)(visible.y + map_offset.y); - int maxX = (int)(visible.x + visible.width - map_offset.x + 1); - int maxY = (int)(visible.y + visible.height - map_offset.y + 1); - - for (int x = minX; x < maxX; x+=scale) { - for (int y = minY; y < maxY; y+=scale) { - sbatch.draw(texture, x , y, scale, scale); - } - } - - } - -} diff --git a/core/src/de/samdev/cannonshooter/level/StandardLevel.java b/core/src/de/samdev/cannonshooter/level/StandardLevel.java index 70f9e5d..1b1ac24 100644 --- a/core/src/de/samdev/cannonshooter/level/StandardLevel.java +++ b/core/src/de/samdev/cannonshooter/level/StandardLevel.java @@ -20,9 +20,7 @@ public class StandardLevel extends GameLayer { setMapScaleResolver(new ShowCompleteMapScaleResolver()); addEntity(new Cannon(7, 13)); - addEntity(new Cannon(14, 5)); - addEntity(new Cannon(20, 13)); } diff --git a/core/src/de/samdev/cannonshooter/util/MathUtils.java b/core/src/de/samdev/cannonshooter/util/MathUtils.java new file mode 100644 index 0000000..8b1685a --- /dev/null +++ b/core/src/de/samdev/cannonshooter/util/MathUtils.java @@ -0,0 +1,16 @@ +package de.samdev.cannonshooter.util; + +public final class MathUtils { + + public static int moduloSignum(float a, float b, float mod, int fallback) { + if (a == b) return 0; + + float d_positive = ((a - b) + 2*mod) % mod; + float d_negative = ((b - a) + 2*mod) % mod; + + if (d_positive > d_negative) return 1; + else if (d_positive < d_negative) return -1; + else return fallback; + } + +}