Permalink
Browse files

Lesson 13: character blinking on damage

  • Loading branch information...
vladimirslav committed Oct 6, 2016
1 parent a337f15 commit 04b22909fe89ef16ceff444db690d60fe14884da
@@ -89,6 +89,7 @@ public void update(float delta)
{
gameTime += delta;
effectEngine.update(delta);
player.update(delta);
enemy.update(delta);
if (lastBonusSpawnTime + BONUS_SPAWN_INTERVAL < gameTime &&
@@ -0,0 +1,60 @@
package com.coldwild.dodginghero.logic.objects;
import com.badlogic.gdx.graphics.g2d.Sprite;
/**
* Created by comrad_gremlin on 10/6/2016.
*/
public class Character extends Sprite {
protected int lives;
protected float timeAlive;
private float timeOfDmgTaken;
public static final float BLINK_TIME_AFTER_DMG = 0.25f;
public Character(int _lives)
{
lives = _lives;
timeAlive = 0;
timeOfDmgTaken = -1;
}
public int getLives()
{
return lives;
}
public void preDraw()
{
if (timeAlive < timeOfDmgTaken + BLINK_TIME_AFTER_DMG)
{
float t = (timeAlive - timeOfDmgTaken) / BLINK_TIME_AFTER_DMG; // 0..1
t = t * t;
setColor(1, 1, 1, t);
}
}
public void postDraw()
{
setColor(1, 1, 1, 1);
}
public void takeDamage(int amount) {
timeOfDmgTaken = timeAlive;
lives -= amount;
if (lives < 0)
{
lives = 0;
}
}
public void update(float delta)
{
timeAlive += delta;
}
}
@@ -11,7 +11,7 @@
/**
* Created by comrad_gremlin on 9/12/2016.
*/
public class Enemy extends Sprite {
public class Enemy extends Character {
private static final float BASE_ATTACK_TIME = 3.0f;
private static final int DEFAULT_ENEMY_LIVES = 10;
@@ -22,14 +22,6 @@
private boolean targetTiles[][];
public void takeDamage(int amount) {
lives -= amount;
if (lives < 0)
{
lives = 0;
}
}
public interface EnemyAttackListener
{
void OnAttack(boolean[][] tiles);
@@ -39,7 +31,7 @@ public void takeDamage(int amount) {
public Enemy(Resources res, EnemyAttackListener listener)
{
lives = DEFAULT_ENEMY_LIVES;
super(DEFAULT_ENEMY_LIVES);
set(res.enemy);
resetAttackTime();
attackListener = listener;
@@ -59,12 +51,16 @@ public void resetAttackTime()
public void draw(SpriteBatch batch, SizeEvaluator sizeEvaluator)
{
preDraw();
setPosition(sizeEvaluator.getEnemyX(this), sizeEvaluator.getEnemyY(this));
super.draw(batch);
postDraw();
}
@Override
public void update(float delta)
{
super.update(delta);
timeSinceAttack += delta;
if (timeSinceAttack > nextAttackTime)
{
@@ -86,9 +82,4 @@ public void update(float delta)
resetAttackTime();
}
}
public int getLives()
{
return lives;
}
}
@@ -8,20 +8,20 @@
/**
* Created by comrad_gremlin on 9/8/2016.
*/
public class Player extends Sprite {
public class Player extends Character {
private int fieldX;
private int fieldY;
private int lives;
private final int max_lives;
public Player(int fx, int fy, Resources res, int _lives)
{
super(_lives);
fieldX = fx;
fieldY = fy;
set(res.player);
lives = _lives;
max_lives = _lives;
}
@@ -47,23 +47,11 @@ public void setFieldY(int fy)
public void draw(SpriteBatch batch, SizeEvaluator sizeEvaluator)
{
preDraw();
setPosition(sizeEvaluator.getBaseScreenX(fieldX),
sizeEvaluator.getBaseScreenY(fieldY));
super.draw(batch);
}
public void takeDamage(int val)
{
lives -= val;
if (lives < 0)
{
lives = 0;
}
}
public int getLives()
{
return lives;
postDraw();
}
public void addLives(int amount) {

0 comments on commit 04b2290

Please sign in to comment.