Permalink
Browse files

Lesson 18: multiple enemy types

  • Loading branch information...
vladimirslav committed Nov 28, 2016
1 parent 21dfa37 commit 012b57d39ed7c12e902acc83a5e7f351069753db
@@ -1,6 +1,6 @@
game.png
size: 128,32
size: 256,32
format: RGBA8888
filter: Nearest,Nearest
repeat: none
@@ -18,44 +18,72 @@ base
orig: 16, 4
offset: 0, 0
index: -1
ground
bat
rotate: false
xy: 20, 8
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
ghost
rotate: false
xy: 38, 8
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
ground
rotate: false
xy: 56, 8
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
health
rotate: false
xy: 110, 10
xy: 182, 10
size: 14, 14
orig: 14, 14
offset: 0, 0
index: -1
player
rotate: false
xy: 38, 8
xy: 74, 8
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
skeleton
rotate: false
xy: 92, 8
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
slime
rotate: false
xy: 110, 8
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
spider
rotate: false
xy: 56, 8
xy: 128, 8
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
wall
rotate: false
xy: 74, 8
xy: 146, 8
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
warning
rotate: false
xy: 92, 8
xy: 164, 8
size: 16, 16
orig: 16, 16
offset: 0, 0
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
@@ -6,11 +6,19 @@
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import java.util.HashMap;
/**
* Created by comrad_gremlin on 9/6/2016.
*/
public class Resources {
public static final int ENEMY_VERTICAL = 0; // spider
public static final int ENEMY_HORIZONTAL = 1; // ghost
public static final int ENEMY_DIAGONAL = 2; // bat
public static final int ENEMY_RANDOM = 3; // slime
public static final int ENEMY_UNIVERSAL = 4; // skeleton
TextureAtlas gameSprites;
public BitmapFont gamefont;
@@ -19,7 +27,7 @@
public TextureRegion wall;
public Sprite player;
public Sprite enemy;
public HashMap<Integer, Sprite> enemySprites;
public TextureRegion base;
public TextureRegion warning;
@@ -38,7 +46,13 @@ public Resources()
wall = gameSprites.findRegion("wall");
player = new Sprite(gameSprites.findRegion("player"));
enemy = new Sprite(gameSprites.findRegion("spider"));
enemySprites = new HashMap<Integer, Sprite>();
enemySprites.put(ENEMY_VERTICAL, gameSprites.createSprite("spider"));
enemySprites.put(ENEMY_HORIZONTAL, gameSprites.createSprite("ghost"));
enemySprites.put(ENEMY_DIAGONAL, gameSprites.createSprite("bat"));
enemySprites.put(ENEMY_RANDOM, gameSprites.createSprite("slime"));
enemySprites.put(ENEMY_UNIVERSAL, gameSprites.createSprite("skeleton"));
base = gameSprites.findRegion("base");
warning = gameSprites.findRegion("warning");
@@ -10,7 +10,7 @@
*/
public class WarningEffect extends Effect {
private static final float WARNING_TIME = 0.5f;
private static final float WARNING_TIME = 0.75f;
private int fieldX;
private int fieldY;
private Resources resources;
@@ -3,6 +3,7 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.MathUtils;
import com.coldwild.dodginghero.DodgingHero;
import com.coldwild.dodginghero.Resources;
import com.coldwild.dodginghero.graph.effects.EffectEngine;
import com.coldwild.dodginghero.graph.effects.WarningEffect;
import com.coldwild.dodginghero.logic.objects.Bonus;
@@ -47,7 +48,7 @@ public GameLogic(DodgingHero _game, GameEventListener _listener) {
GameProgress.playerLives
); // 0..3
enemy = new Enemy(game.res, this);
enemy = new Enemy(game.res, this, MathUtils.random(Resources.ENEMY_UNIVERSAL));
effectEngine = new EffectEngine();
bonuses = new ArrayList<Bonus>();
@@ -14,7 +14,8 @@
*/
public class Enemy extends Character {
private static final float BASE_ATTACK_TIME = 3.0f;
private static final float BASE_ATTACK_TIME = 1.0f;
private static final float WARM_UP_TIME = 2.0f;
private float timeSinceAttack;
private float nextAttackTime;
@@ -31,10 +32,14 @@
private EnemyAttackListener attackListener;
public Enemy(Resources res, EnemyAttackListener listener)
private int type;
public Enemy(Resources res, EnemyAttackListener listener, int _type)
{
super(GameProgress.getEnemyLives());
set(res.enemy);
type = _type;
set(res.enemySprites.get(type));
resetAttackTime();
attackListener = listener;
@@ -74,24 +79,144 @@ public void update(float delta)
{
super.update(delta);
timeSinceAttack += delta;
if (timeSinceAttack > nextAttackTime)
if (timeAlive > WARM_UP_TIME && timeSinceAttack > nextAttackTime)
{
int col1 = MathUtils.random(GameLogic.MAX_BASE_X);
int col2 = 0;
do {
col2 = MathUtils.random(GameLogic.MAX_BASE_X);
} while (col1 == col2);
for (int x = 0; x < GameLogic.MAX_BASE_X + 1; x++)
switch (type)
{
for (int y = 0; y < GameLogic.MAX_BASE_Y +1; y++)
{
targetTiles[x][y] = (x == col1 || x == col2);
}
case Resources.ENEMY_VERTICAL:
performVerticalLineAttack();
break;
case Resources.ENEMY_HORIZONTAL:
performHorizontalLineAttack();
break;
case Resources.ENEMY_DIAGONAL:
performDiagonalAttack();
break;
case Resources.ENEMY_RANDOM:
performRandomAttack();
break;
default:
performUltimateAttack();
break;
}
attackListener.OnAttack(targetTiles);
resetAttackTime();
}
}
private void performVerticalLineAttack()
{
int col1 = MathUtils.random(GameLogic.MAX_BASE_X);
int col2 = 0;
do {
col2 = MathUtils.random(GameLogic.MAX_BASE_X);
} while (col1 == col2);
for (int x = 0; x < GameLogic.MAX_BASE_X + 1; x++)
{
for (int y = 0; y < GameLogic.MAX_BASE_Y +1; y++)
{
targetTiles[x][y] = (x == col1 || x == col2);
}
}
}
private void performHorizontalLineAttack()
{
int row1 = MathUtils.random(GameLogic.MAX_BASE_Y);
int row2 = 0;
do {
row2 = MathUtils.random(GameLogic.MAX_BASE_Y);
} while (row1 == row2);
for (int x = 0; x < GameLogic.MAX_BASE_X + 1; x++)
{
for (int y = 0; y < GameLogic.MAX_BASE_Y +1; y++)
{
targetTiles[x][y] = (y == row1 || y == row2);
}
}
}
private void fillDiagonal(int xstart, int dx)
{
for (int i = 0; i <= GameLogic.MAX_BASE_Y; i++)
{
int nx = xstart + i * dx;
if (nx > GameLogic.MAX_BASE_X)
{
nx = nx - GameLogic.MAX_BASE_X - 1;
}
if (nx < 0)
{
nx = nx + GameLogic.MAX_BASE_X + 1;
}
targetTiles[nx][i] = true;
}
}
private void performDiagonalAttack()
{
int dx1 = -1 + MathUtils.random(1) * 2; // 1 .. -1
int dx2 = -1 + MathUtils.random(1) * 2; // 1 .. -1
int col1 = MathUtils.random(GameLogic.MAX_BASE_X);
int col2 = 0;
do {
col2 = MathUtils.random(GameLogic.MAX_BASE_X);
} while (col1 == col2);
for (int x = 0; x < GameLogic.MAX_BASE_X + 1; x++)
{
for (int y = 0; y < GameLogic.MAX_BASE_Y +1; y++)
{
targetTiles[x][y] = false;
}
}
fillDiagonal(col1, dx1);
fillDiagonal(col2, dx2);
}
private void performRandomAttack()
{
for (int x = 0; x < GameLogic.MAX_BASE_X + 1; x++)
{
for (int y = 0; y < GameLogic.MAX_BASE_Y +1; y++)
{
targetTiles[x][y] = false;
}
}
for (int i = 0; i < 10; i++)
{
int nx = MathUtils.random(GameLogic.MAX_BASE_X);
int ny = MathUtils.random(GameLogic.MAX_BASE_Y);
targetTiles[nx][ny] = true;
}
}
private void performUltimateAttack()
{
int rnd = MathUtils.random(3);
switch (rnd)
{
case 0:
performVerticalLineAttack();
break;
case 1:
performHorizontalLineAttack();
break;
case 2:
performDiagonalAttack();
break;
default:
performRandomAttack();
break;
}
}
}
@@ -179,7 +179,6 @@ public void render(float delta)
bg.draw(gameStage, game.res);
drawBases();
logic.getEffectEngine().draw(batch, sizeEvaluator);
batch.begin();
for (Bonus bonus : logic.getBonuses())
@@ -190,6 +189,8 @@ public void render(float delta)
logic.getEnemy().draw(batch, sizeEvaluator);
batch.end();
logic.getEffectEngine().draw(batch, sizeEvaluator);
gameStage.getCamera().position.set(gameStage.getWidth() / 2, gameStage.getHeight() / 2, 0);
if (player.getLives() > 0 &&
player.getTimeAlive() - player.getTimeOfDmgTaken() < SHAKE_TIME_ON_DMG)

0 comments on commit 012b57d

Please sign in to comment.