Added flying bullets

This commit is contained in:
Mike Schwörer 2015-09-17 21:02:27 +02:00
parent bfc181b8a5
commit 18d1aa4977
10 changed files with 144 additions and 26 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 31 KiB

View File

@ -17,7 +17,7 @@ public final class Textures {
texbackground = new Texture("level_background.png"); texbackground = new Texture("level_background.png");
cannon_body = new Texture("cannon_body.png"); cannon_body = new Texture("cannon_body.png");
cannon_barrel = TextureHelper.load1DArray("cannon_barrel.png", 512, 256, 16); cannon_barrel = TextureHelper.load1DArray("cannon_barrel.png", 512, 256, 32);
cannon_hearth = TextureHelper.load1DArray("cannon_hearth.png", 256, 256, 64); cannon_hearth = TextureHelper.load1DArray("cannon_hearth.png", 256, 256, 64);
cannon_bullet = new Texture("cannon_bullet.png"); cannon_bullet = new Texture("cannon_bullet.png");
} }

View File

@ -17,7 +17,7 @@ public class Cannon extends Entity {
private CannonBarrel barrel; private CannonBarrel barrel;
private CannonHearth hearth; private CannonHearth hearth;
public float power; // 1 = active | 0 = neutral public float health; // 1 = active | 0 = neutral
public Cannon(float x, float y, Team t) { public Cannon(float x, float y, Team t) {
super(Textures.cannon_body, 2, 2); super(Textures.cannon_body, 2, 2);
@ -26,7 +26,7 @@ public class Cannon extends Entity {
setZLayer(ZLayers.LAYER_CANNON_BODY); setZLayer(ZLayers.LAYER_CANNON_BODY);
team = t; team = t;
power = (t.isNeutral) ? 0 : 1; health = (t.isNeutral) ? 0 : 1;
} }
@Override @Override
@ -44,12 +44,12 @@ public class Cannon extends Entity {
{ {
if (isMouseOverEntity() && Gdx.input.isKeyPressed(Keys.DOWN) && ! team.isNeutral) if (isMouseOverEntity() && Gdx.input.isKeyPressed(Keys.DOWN) && ! team.isNeutral)
{ {
power = Math.max(0, power - 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)
{ {
power = Math.min(1, power + 0.01f); health = Math.min(1, health + 0.01f);
} }
} }
@ -60,7 +60,11 @@ public class Cannon extends Entity {
} }
public void setTeam(Team newteam) { public void setTeam(Team newteam) {
team = newteam; if (team != newteam) {
team = newteam;
barrel.onTeamChanged();
}
} }
@Override @Override

View File

@ -1,6 +1,7 @@
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.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;
@ -12,7 +13,8 @@ import de.samdev.cannonshooter.ZLayers;
import de.samdev.cannonshooter.util.MathUtils; import de.samdev.cannonshooter.util.MathUtils;
public class CannonBarrel extends Entity { public class CannonBarrel extends Entity {
private static final float CHARGE_DURATION = 0.0001f; 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 ROTATION_SPEED = 0.18f;
private boolean dragging = false; private boolean dragging = false;
@ -20,6 +22,9 @@ public class CannonBarrel extends Entity {
private float rotation = 0; private float rotation = 0;
private float targetRotation = 0; private float targetRotation = 0;
private float charge = 0; private float charge = 0;
private boolean loaded = false;
private CannonBullet bullet = null;
private Cannon cannon; private Cannon cannon;
@ -31,11 +36,60 @@ public class CannonBarrel extends Entity {
setZLayer(ZLayers.LAYER_CANNON_BARREL); setZLayer(ZLayers.LAYER_CANNON_BARREL);
} }
@Override
public TextureRegion getTexture() {
return Textures.cannon_barrel[31 - Math.min(31, (int)(charge * 32))];
}
@Override @Override
public void beforeUpdate(float delta) { public void beforeUpdate(float delta) {
if (dragging) updateDragging(); if (dragging) updateDragging();
updateRotation(delta);
updateCharge(delta);
updateBullet();
}
private void updateBullet() {
if (loaded) {
Vector2 v = new Vector2(1f - 0.2f + (120*charge)/128f, 0);
v.rotate(rotation);
bullet.setPosition(cannon.getCenterX() + v.x - bullet.getWidth()/2, cannon.getCenterY() + v.y - bullet.getHeight()/2);
}
}
private void updateCharge(float delta) {
if (cannon.health == 0 || cannon.health == 1) {
if (loaded)
{
charge += CHARGE_SPEED * delta * cannon.team.speedMultiplier;
if (charge > 1) {
charge = 0;
bullet.shoot(rotation);
bullet = null;
loaded = false;
}
} else {
charge = 0;
bullet = new CannonBullet(cannon, cannon.team);
owner.addEntity(bullet);
loaded = true;
}
} else {
charge -= UNCHARGE_SPEED * delta;
if (charge < 0)
charge = 0;
}
}
private void updateRotation(float delta) {
if (rotation != targetRotation) { if (rotation != targetRotation) {
float sign = MathUtils.moduloSignum(rotation, targetRotation, 360, 1); float sign = MathUtils.moduloSignum(rotation, targetRotation, 360, 1);
@ -66,6 +120,13 @@ public class CannonBarrel extends Entity {
dragging = true; dragging = true;
} }
public void onTeamChanged() {
if (bullet != null) bullet.kill();
bullet = null;
loaded = false;
}
@Override @Override
public void onLayerAdd(GameLayer layer) { public void onLayerAdd(GameLayer layer) {
// //

View File

@ -1,6 +1,7 @@
package de.samdev.cannonshooter.entities; package de.samdev.cannonshooter.entities;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Rectangle;
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;
@ -8,21 +9,79 @@ import de.samdev.absgdx.framework.entities.colliosiondetection.geometries.Collis
import de.samdev.absgdx.framework.layer.GameLayer; import de.samdev.absgdx.framework.layer.GameLayer;
import de.samdev.cannonshooter.Textures; import de.samdev.cannonshooter.Textures;
import de.samdev.cannonshooter.ZLayers; import de.samdev.cannonshooter.ZLayers;
import de.samdev.cannonshooter.teams.Team;
public class CannonBullet extends Entity { public class CannonBullet extends Entity {
private Cannon cannon; private final static float RESIZE_SPEED = 0.005f;
public CannonBullet(Cannon owner) { private Cannon cannon;
private final Team team;
private boolean inBarrel = true;
private float scale = 0.001f;
private boolean death = false;
private boolean birth = true;
public CannonBullet(Cannon owner, Team t) {
super(Textures.cannon_bullet, 0.25f, 0.25f); super(Textures.cannon_bullet, 0.25f, 0.25f);
cannon = owner; this.cannon = owner;
this.team = t;
setPosition(cannon.getPositionX(), cannon.getPositionY()); setPosition(cannon.getPositionX(), cannon.getPositionY());
setZLayer(ZLayers.LAYER_CANNON_BULLET); setZLayer(ZLayers.LAYER_CANNON_BULLET);
setColorTint(Color.RED); setColorTint(team.teamColor);
} }
@Override
public void beforeUpdate(float delta) {
if (birth) {
scale += RESIZE_SPEED * delta;
if (scale >= 1) {
birth = false;
scale = 1;
}
}
if (death) {
scale -= RESIZE_SPEED * delta;
if (scale <= 0) {
death = false;
scale = 0.0001f;
alive = false;
}
}
// TODO OPTIMIZE
// TODO Add max lifetime (=> kill() )
if (! owner.getVisibleMapBox().overlaps(new Rectangle(getPositionX(), getPositionY(), getWidth(), getHeight())))
{
alive = false;
}
}
public void shoot(float rotation) {
inBarrel = false;
speed.set(0.001f, 0);
speed.rotate(rotation);
}
public void kill() {
death = true;
}
@Override
public float getTextureScaleX() {
return scale;
}
@Override
public float getTextureScaleY() {
return scale;
}
@Override @Override
public void onLayerAdd(GameLayer layer) { public void onLayerAdd(GameLayer layer) {
// //
@ -58,9 +117,4 @@ public class CannonBullet extends Entity {
return false; return false;
} }
@Override
public void beforeUpdate(float delta) {
//
}
} }

View File

@ -38,20 +38,20 @@ public class CannonHearth extends Entity {
sbatch.setColor(cannon.team.teamColor); sbatch.setColor(cannon.team.teamColor);
renderTexture(sbatch, Textures.cannon_hearth[(int)(cannon.power * 63)], 0, 0); renderTexture(sbatch, Textures.cannon_hearth[(int)(cannon.health * 63)], 0, 0);
sbatch.setColor(Color.WHITE); sbatch.setColor(Color.WHITE);
} }
@Override @Override
public void beforeUpdate(float delta) { public void beforeUpdate(float delta) {
if (cannon.power < 1) { if (cannon.health < 1) {
if (rotation != 0 && cannon.power > 0) { if (rotation != 0 && cannon.health > 0) {
rotation = (rotation - delta * ROTATION_SPEED * cannon.team.speedMultiplier); rotation = (rotation - delta * ROTATION_SPEED * cannon.team.speedMultiplier);
if (rotation < 0) rotation = 0; if (rotation < 0) rotation = 0;
} }
if (cannon.power == 0){ if (cannon.health == 0){
if (! cannon.team.isNeutral) cannon.setTeam(level.team_neutral); if (! cannon.team.isNeutral) cannon.setTeam(level.team_neutral);
rotation = (rotation - delta * ROTATION_SPEED * cannon.team.speedMultiplier); rotation = (rotation - delta * ROTATION_SPEED * cannon.team.speedMultiplier);

View File

@ -3,7 +3,6 @@ package de.samdev.cannonshooter.level;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import de.samdev.absgdx.framework.AgdxGame; import de.samdev.absgdx.framework.AgdxGame;

View File

@ -4,7 +4,7 @@ import com.badlogic.gdx.graphics.Color;
public class Team { public class Team {
public static final Color COL_NEUTRAL = new Color(127/255f, 127/255f, 127/255f, 1.0f); public static final Color COL_NEUTRAL = new Color(208/255f, 208/255f, 208/255f, 1.0f);
public static final Color COL_P1 = new Color(38/255f, 127/255f, 0/255f, 1.0f); public static final Color COL_P1 = new Color(38/255f, 127/255f, 0/255f, 1.0f);
public static final Color COL_P2 = new Color(255/255f, 0/255f, 0/255f, 1.0f); public static final Color COL_P2 = new Color(255/255f, 0/255f, 0/255f, 1.0f);
public static final Color COL_P3 = new Color(0/255f, 0/255f, 255/255f, 1.0f); public static final Color COL_P3 = new Color(0/255f, 0/255f, 255/255f, 1.0f);

View File

@ -7,19 +7,19 @@ void Main()
{ {
Image input = Image.FromFile(@"F:\Eigene Dateien\Dropbox\Programming\Java\workspace\Cannon Shooter\data\cannon_barrel_base.png"); Image input = Image.FromFile(@"F:\Eigene Dateien\Dropbox\Programming\Java\workspace\Cannon Shooter\data\cannon_barrel_base.png");
Image output = new Bitmap(input.Width * 4, input.Height * 4, PixelFormat.Format32bppArgb); Image output = new Bitmap(input.Width * 4, input.Height * 8, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(output)) using (Graphics g = Graphics.FromImage(output))
{ {
for (int x = 0; x < 4; x++) for (int x = 0; x < 4; x++)
{ {
for (int y = 0; y < 4; y++) for (int y = 0; y < 8; y++)
{ {
int idx = y*4 + x; int idx = y*4 + x;
g.DrawImageUnscaledAndClipped(input, new Rectangle(input.Width * x, input.Height * y, input.Width - idx * 8 - 22, input.Height)); g.DrawImageUnscaledAndClipped(input, new Rectangle(input.Width * x, input.Height * y, input.Width - idx * 4 - 22, input.Height));
g.DrawImage(input, new Rectangle(input.Width * (x+1) - idx * 8 - 22, input.Height * y, 22, input.Height), new Rectangle(input.Width - 22, 0, 22, input.Height), GraphicsUnit.Pixel); g.DrawImage(input, new Rectangle(input.Width * (x+1) - idx * 4 - 22, input.Height * y, 22, input.Height), new Rectangle(input.Width - 22, 0, 22, input.Height), GraphicsUnit.Pixel);
} }
} }
} }

Binary file not shown.