Added Rotate Gesture to Barrel

This commit is contained in:
Mike Schwörer 2015-09-17 19:07:49 +02:00
parent 83a80700db
commit efcd34ebff
9 changed files with 185 additions and 140 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -19,5 +19,6 @@ public final class Textures {
cannon_body = new Texture("cannon_body.png");
cannon_barrel = TextureHelper.load1DArray("cannon_barrel.png", 512, 256, 16);
cannon_hearth = TextureHelper.load1DArray("cannon_hearth.png", 256, 256, 64);
cannon_bullet = new Texture("cannon_bullet.png");
}
}

View File

@ -1,7 +1,7 @@
package de.samdev.cannonshooter.entities;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import de.samdev.absgdx.framework.entities.Entity;
import de.samdev.absgdx.framework.entities.colliosiondetection.CollisionGeometryOwner;
@ -36,39 +36,43 @@ public class Cannon extends Entity {
@Override
public void beforeUpdate(float delta) {
if (isMouseOverEntity() && Gdx.input.justTouched() && Gdx.input.isButtonPressed(Buttons.LEFT) && power > 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

View File

@ -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
}
}

View File

@ -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) {
//
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}
}
}

View File

@ -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));
}

View File

@ -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;
}
}