From adbfa3435065ae65763d25fbac123089e3bfd220 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Tue, 17 Apr 2007 12:51:26 +0000 Subject: [PATCH] Added health bar --- src/ei/game/scene/GameEntity.java | 44 +++++++++++++++----- src/ei/game/scene/SelectBox.java | 69 +++++++++++++++++++++++++++++++ src/ei/game/scene/units/Tank.java | 20 +++------ src/ei/game/scene/units/Unit.java | 6 ++- 4 files changed, 113 insertions(+), 26 deletions(-) create mode 100644 src/ei/game/scene/SelectBox.java diff --git a/src/ei/game/scene/GameEntity.java b/src/ei/game/scene/GameEntity.java index 9ff3e64..44a093f 100644 --- a/src/ei/game/scene/GameEntity.java +++ b/src/ei/game/scene/GameEntity.java @@ -1,14 +1,14 @@ package ei.game.scene; import ei.engine.scene.Entity; -import ei.engine.scene.Node; public abstract class GameEntity{ + private int max_life; private int life; private boolean selected; public GameEntity(int l){ - life = l; + setLife(l); this.selected = false; } @@ -20,6 +20,37 @@ public abstract class GameEntity{ public int getLife(){ return life; } + + + /** + * Set the life + * @param l The life to set to + */ + public void setLife(int l){ + life = l; + if(life > max_life){ + max_life = life; + } + } + + /** + * Returns the max life + * + * @return The max life + */ + public int getMaxLife(){ + return max_life; + } + + + /** + * Set the max life + * @param l The max life + */ + public void setMaxLife(int l){ + max_life = l; + } + /** * If the unit is selected. * @return true if selected. @@ -34,15 +65,8 @@ public abstract class GameEntity{ public void setSelected(boolean b) { this.selected = b; } - protected abstract Entity getSelection(); + protected abstract SelectBox getSelection(); - /** - * Set the life - * @param l The life to set to - */ - public void setLife(int l){ - life = l; - } public void move(int x, int y) { } diff --git a/src/ei/game/scene/SelectBox.java b/src/ei/game/scene/SelectBox.java new file mode 100644 index 0000000..4e4cf05 --- /dev/null +++ b/src/ei/game/scene/SelectBox.java @@ -0,0 +1,69 @@ +package ei.game.scene; + +import ei.engine.math.Vector2f; +import ei.engine.math.Vector4f; +import ei.engine.scene.Box; +import ei.engine.scene.Node; +import ei.engine.scene.Sprite; +import ei.engine.texture.Texture; + +public class SelectBox { + private Node selectNode; + private Sprite value; + private int max; + private float whidth; + private static final float hight = 3; + + /** + * Creates a selection box with health bar + * + * @param x The size of the selection box + * @param y The size of the selection box + * @param max The max health of the object + */ + public SelectBox(float x, float y, int max){ + this.max = max; + this.whidth = x; + selectNode = new Node("SelectionNode"); + + Texture tex = new Texture(); + tex.setColor(new Vector4f(0.5f, 1.0f, 0.5f,1)); + + Texture tex2 = new Texture(); + tex2.setColor(new Vector4f(0.3f, 1.0f, 0.3f, 0.6f)); + + value = new Sprite("Healthvalue",tex2); + value.setSize(new Vector2f(x,hight)); + value.setLocation(new Vector2f(0,-x/2+hight/2)); + selectNode.add(value); + + Box bar = new Box("bar",tex); + bar.setSize(new Vector2f(x,hight)); + bar.setLocation(new Vector2f(0,-x/2+hight/2)); + selectNode.add(bar); + + Box select = new Box("select",tex); + select.setSize(new Vector2f(x,y)); + selectNode.add(select); + + + } + + /** + * Returns the node of the selection + * + * @return The node of the selection + */ + public Node getNode(){ + return selectNode; + } + + /** + * Sets the value of the health bar + * + * @param v The value of the health bar + */ + public void setValue(int v){ + value.setSize(new Vector2f((v/max)*whidth,hight)); + } +} diff --git a/src/ei/game/scene/units/Tank.java b/src/ei/game/scene/units/Tank.java index a5678b6..41c85b1 100644 --- a/src/ei/game/scene/units/Tank.java +++ b/src/ei/game/scene/units/Tank.java @@ -2,14 +2,11 @@ package ei.game.scene.units; import ei.engine.math.Vector2f; import ei.engine.math.Vector2i; -import ei.engine.math.Vector4f; -import ei.engine.scene.Box; -import ei.engine.scene.Entity; import ei.engine.scene.Sprite; -import ei.engine.texture.Texture; +import ei.game.scene.SelectBox; public class Tank extends Unit{ - private Box box; + private SelectBox selectionBox; public Tank() { this(0, 0); } @@ -19,17 +16,12 @@ public class Tank extends Unit{ Sprite sp = new Sprite("Tank", "data/units/tank.png"); sp.setSize(new Vector2f(50,37)); getNode().add(sp); - - Texture tex = new Texture(); - tex.setColor(new Vector4f(0.5f, 1.0f, 0.5f,1)); - box = new Box("selectionBox",tex); - box.setSize(new Vector2f(40,40)); - + + selectionBox = new SelectBox(40,40,getMaxLife()); } - protected Entity getSelection() { - - return box; + protected SelectBox getSelection() { + return selectionBox; } diff --git a/src/ei/game/scene/units/Unit.java b/src/ei/game/scene/units/Unit.java index 4fca1b7..3b02452 100644 --- a/src/ei/game/scene/units/Unit.java +++ b/src/ei/game/scene/units/Unit.java @@ -45,10 +45,10 @@ public abstract class Unit extends GameEntity{ public void setSelected(boolean b) { super.setSelected(b); if(b) { - unitNode.add(getSelection()); + unitNode.add(getSelection().getNode()); } else{ - unitNode.remove(getSelection()); + unitNode.remove(getSelection().getNode()); } } @@ -94,6 +94,8 @@ public abstract class Unit extends GameEntity{ * Updating the unit */ public void update() { + getSelection().setValue(getLife()); + if(moveTo != null) { Vector2i moveVect = Map.getPosByPixel(moveTo.getX(), moveTo.getY()); Vector2i currentVect = Map.getPosByPixel(unitNode.getLocation().getX(), unitNode.getLocation().getY());