From 092dc176a859aca6037c6cb4d5841cda3185e5ee Mon Sep 17 00:00:00 2001 From: Niklas Ulf Anders Cullberg Date: Wed, 17 Oct 2012 11:20:09 +0200 Subject: [PATCH] Added Stats, damage code, stuff --- DepthsBelow/DepthsBelow/Component/Stat.cs | 43 +++- DepthsBelow/DepthsBelow/SmallEnemy.cs | 288 +++++++++++----------- DepthsBelow/DepthsBelow/Soldier.cs | 6 + DepthsBelow/DepthsBelow/Utility.cs | 16 +- 4 files changed, 205 insertions(+), 148 deletions(-) diff --git a/DepthsBelow/DepthsBelow/Component/Stat.cs b/DepthsBelow/DepthsBelow/Component/Stat.cs index 2a00b2a..4412f8a 100644 --- a/DepthsBelow/DepthsBelow/Component/Stat.cs +++ b/DepthsBelow/DepthsBelow/Component/Stat.cs @@ -7,23 +7,56 @@ namespace DepthsBelow.Component { public class Stat : Component { + protected int hp = 0; + protected int defence = 0; protected int stepsTaken = 0; protected int distanceToTarget = 0; protected int panic = 0; - protected int soldierAim = 0; + protected int soldierAim = 100; protected int weaponAccuracy = 0; + protected int weaponStrength = 0; + protected int ammo = 0; protected int enemyDodge = 0; protected int penaltyRange = 5; - public int GetAim() + public int GetAim { - return (soldierAim + weaponAccuracy); + get { return soldierAim + weaponAccuracy; } + set { soldierAim = value; } } - public int GetDodge() + public int GetDodge { - return enemyDodge; + get { return enemyDodge; } + set { enemyDodge = value; } + } + public int Life + { + get { return hp; } + set + { + hp = value; + if (hp <= 0) { + //If you kill me, I will become stronger than you can ever imagine. + Kill(); + } + } + } + public int Defence + { + get { return defence; } + set { defence = value; } + } + public int Strength + { + get { return weaponStrength; } + set { weaponStrength = value; } + } + + public void Kill() + { + hp = 0; } public int Penalty(int distance) diff --git a/DepthsBelow/DepthsBelow/SmallEnemy.cs b/DepthsBelow/DepthsBelow/SmallEnemy.cs index b1b4aa2..c171653 100644 --- a/DepthsBelow/DepthsBelow/SmallEnemy.cs +++ b/DepthsBelow/DepthsBelow/SmallEnemy.cs @@ -1,142 +1,148 @@ -using System; -using System.Collections.Generic; -using DepthsBelow.Component; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework.Input; - -namespace DepthsBelow -{ - public class SmallEnemy : Entity - { - public static Texture2D Texture; - public Color Color; - public static Point Origin; - private bool _selected; //I guess as in "currently doing something" - - public List Swarm; - - private PathFinder.Node nextNode; - - public int numberOfSteps = 5; - public int currentStep = 0; - - public int step - { - get { return currentStep; } - set - { - currentStep = value; - } - } - - public SmallEnemy(Core core, ref List swarm) - : base(core) - { - if (Texture == null) - LoadContent(core); - - this.Swarm = swarm; - - Transform.World.Origin = new Vector2(20, 16); - - this.Color = Color.White; - var rc = new SpriteRenderer(this) { Texture = Texture, Color = Color.White }; - AddComponent(rc); - - var cc = new Collision(this, 32, 32); - AddComponent(cc); - - var pfc = new PathFinder(this); - AddComponent(pfc); - } - - public bool Selected - { - get { return _selected; } - set - { - _selected = value; - GetComponent().Color = (value) ? Color.Blue : this.Color; - } - } - - public static void LoadContent(Core core) - { - Texture = core.Content.Load("images/Monster"); - } - - public override void Update(GameTime gameTime) - { - base.Update(gameTime); - - float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f; - - if (nextNode == null) - nextNode = GetComponent().Next(); - - if (nextNode != null) - { - List soldierCollisions = new List(); - - foreach (var body in Swarm) - { - if (body != this) - { - if (body.Transform.Grid == nextNode.Position) - { - soldierCollisions.Add(body.Transform.Grid); - GetComponent().RecreatePath(soldierCollisions); - nextNode = GetComponent().Next(); - break; - } - } - } - - foreach (var soldier in core.Squad) - { - var pathFinder = soldier.GetComponent(); - var position = nextNode.Position; - if (soldier.Transform.Grid == position) - { - soldierCollisions.Add(soldier.Transform.Grid); - GetComponent().RecreatePath(soldierCollisions); - nextNode = GetComponent().Next(); - break; - } - } - - if (nextNode != null) - { - var nodeWorldPos = Grid.GridToWorld(nextNode.Position); - // HACK: Make this work properly with other speeds... - float speed = 4f; - if (Transform.World.X < nodeWorldPos.X) - Transform.World.X += speed; - if (Transform.World.X > nodeWorldPos.X) - Transform.World.X -= speed; - if (Transform.World.Y < nodeWorldPos.Y) - Transform.World.Y += speed; - if (Transform.World.Y > nodeWorldPos.Y) - Transform.World.Y -= speed; - - if (Transform.World == nodeWorldPos) - { - if (currentStep < numberOfSteps) - { - currentStep++; - //lastLastNode = lastNode; - //lastNode = nextNode; - nextNode = GetComponent().Next(); - } - else - { - GetComponent().Stop(); - } - } - } - - } - } - } +using System; +using System.Collections.Generic; +using DepthsBelow.Component; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; + +namespace DepthsBelow +{ + public class SmallEnemy : Entity + { + public static Texture2D Texture; + public Color Color; + public static Point Origin; + private bool _selected; //I guess as in "currently doing something" + + public List Swarm; + + private PathFinder.Node nextNode; + + public int numberOfSteps = 5; + public int currentStep = 0; + + public int step + { + get { return currentStep; } + set + { + currentStep = value; + } + } + + public SmallEnemy(Core core, ref List swarm) + : base(core) + { + if (Texture == null) + LoadContent(core); + + this.Swarm = swarm; + + Transform.World.Origin = new Vector2(20, 16); + + this.Color = Color.White; + var rc = new SpriteRenderer(this) { Texture = Texture, Color = Color.White }; + AddComponent(rc); + + var cc = new Collision(this, 32, 32); + AddComponent(cc); + + var pfc = new PathFinder(this); + AddComponent(pfc); + + stat.Life = 2; + stat.Defence = 0; + stat.Strength = 5; + stat.GetAim = 100; + stat.GetDodge = 10; + } + + public bool Selected + { + get { return _selected; } + set + { + _selected = value; + GetComponent().Color = (value) ? Color.Blue : this.Color; + } + } + + public static void LoadContent(Core core) + { + Texture = core.Content.Load("images/Monster"); + } + + public override void Update(GameTime gameTime) + { + base.Update(gameTime); + + float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f; + + if (nextNode == null) + nextNode = GetComponent().Next(); + + if (nextNode != null) + { + List soldierCollisions = new List(); + + foreach (var body in Swarm) + { + if (body != this) + { + if (body.Transform.Grid == nextNode.Position) + { + soldierCollisions.Add(body.Transform.Grid); + GetComponent().RecreatePath(soldierCollisions); + nextNode = GetComponent().Next(); + break; + } + } + } + + foreach (var soldier in core.Squad) + { + var pathFinder = soldier.GetComponent(); + var position = nextNode.Position; + if (soldier.Transform.Grid == position) + { + soldierCollisions.Add(soldier.Transform.Grid); + GetComponent().RecreatePath(soldierCollisions); + nextNode = GetComponent().Next(); + break; + } + } + + if (nextNode != null) + { + var nodeWorldPos = Grid.GridToWorld(nextNode.Position); + // HACK: Make this work properly with other speeds... + float speed = 4f; + if (Transform.World.X < nodeWorldPos.X) + Transform.World.X += speed; + if (Transform.World.X > nodeWorldPos.X) + Transform.World.X -= speed; + if (Transform.World.Y < nodeWorldPos.Y) + Transform.World.Y += speed; + if (Transform.World.Y > nodeWorldPos.Y) + Transform.World.Y -= speed; + + if (Transform.World == nodeWorldPos) + { + if (currentStep < numberOfSteps) + { + currentStep++; + //lastLastNode = lastNode; + //lastNode = nextNode; + nextNode = GetComponent().Next(); + } + else + { + GetComponent().Stop(); + } + } + } + + } + } + } } \ No newline at end of file diff --git a/DepthsBelow/DepthsBelow/Soldier.cs b/DepthsBelow/DepthsBelow/Soldier.cs index 074e002..857cc82 100644 --- a/DepthsBelow/DepthsBelow/Soldier.cs +++ b/DepthsBelow/DepthsBelow/Soldier.cs @@ -61,6 +61,12 @@ namespace DepthsBelow var pfc = new PathFinder(this); AddComponent(pfc); + + stat.Life = 10; + stat.Defence = 10; + stat.Strength = 10; + stat.GetAim = 100; + stat.GetDodge = 20; } public bool Selected diff --git a/DepthsBelow/DepthsBelow/Utility.cs b/DepthsBelow/DepthsBelow/Utility.cs index 0f0e713..aa6a870 100644 --- a/DepthsBelow/DepthsBelow/Utility.cs +++ b/DepthsBelow/DepthsBelow/Utility.cs @@ -16,11 +16,23 @@ namespace DepthsBelow { return 0; } - int baseHitChance = shooting.GetAim(); - int baseDodgeChance = dodging.GetDodge(); + int baseHitChance = shooting.GetAim; + int baseDodgeChance = dodging.GetDodge; int basePenalty = shooting.Penalty((int)Vector2.Distance(attacker.GetComponent().World.Position, defender.GetComponent().World.Position) / Grid.TileSize); int chanceToHit = baseHitChance - baseDodgeChance - basePenalty; return chanceToHit; } + public static bool HitTest(Entity attacker, Entity defender, int chanceToHit) + { + Component.Stat shooting = attacker.GetComponent(); + Component.Stat dodging = defender.GetComponent(); + Random rand = new Random(); + if (rand.Next(0, 100) < chanceToHit) + { + dodging.Life -= shooting.Strength - dodging.Defence / 2; + return true; + } + return false; + } } }