diff --git a/DepthsBelow/DepthsBelow/Component/Stat.cs b/DepthsBelow/DepthsBelow/Component/Stat.cs index d8f35c4..ee5e230 100755 --- a/DepthsBelow/DepthsBelow/Component/Stat.cs +++ b/DepthsBelow/DepthsBelow/Component/Stat.cs @@ -89,7 +89,7 @@ namespace DepthsBelow.Component public int Penalty(int distance) { - return panic + GetPenalty(stepsTaken) + (int)(GetPenalty(distance) / 2); + return panic/* + GetPenalty(stepsTaken)*/ + (int)(GetPenalty(distance)); } public Stat(Entity parent) diff --git a/DepthsBelow/DepthsBelow/EntityManager.cs b/DepthsBelow/DepthsBelow/EntityManager.cs index f4a1624..4e1bca6 100755 --- a/DepthsBelow/DepthsBelow/EntityManager.cs +++ b/DepthsBelow/DepthsBelow/EntityManager.cs @@ -85,9 +85,9 @@ namespace DepthsBelow /// public void Update(GameTime gameTime) { - foreach (var entity in Entities.ToList()) - entity.Update(gameTime); - } + foreach (var entity in Entities.ToList()) + entity.Update(gameTime); + } /// /// Draw all entities handled by the entity manager. diff --git a/DepthsBelow/DepthsBelow/KeyboardInput.cs b/DepthsBelow/DepthsBelow/KeyboardInput.cs index 8c21677..401c2d7 100755 --- a/DepthsBelow/DepthsBelow/KeyboardInput.cs +++ b/DepthsBelow/DepthsBelow/KeyboardInput.cs @@ -1,275 +1,298 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Content; -using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework.Input; -using Microsoft.Xna.Framework.Media; - -namespace DepthsBelow -{ - public class KeyboardInput - { - Core core; - private KeyboardState lastKeyboardState; - - int checkerSpeed = 2; - - public KeyboardInput(Core core) - { - this.core = core; - } - - /*public List> GetGroups(List soldiers) - { - var ungrouped = soldiers.ToList(); - var groups = new List>(); - - for (int index = 0; index < soldiers.Count; index++) - { - var checkingSoldier = soldiers[index]; - var group = new List(); - - for (int i = 0; i < soldiers.Count; i++) - { - var soldier = soldiers[i]; - if (ungrouped.Contains(soldier)) - { - float distance = Vector2.Distance(checkingSoldier.Transform.World + checkingSoldier.Transform.World.Origin, - soldier.Transform.World + soldier.Transform.World.Origin); - if (distance <= ((float) Grid.TileSize * 1f)) - { - ungrouped.Remove(soldier); - group.Add(soldier); - } - } - } - - if (group.Count > 0) - groups.Add(group); - } - - return groups; - }*/ - - // Get a list of dynamic soldier groups - public List> GetGroups(List soldiers, float maxRange) - { - var alreadyGrouped = new List(); - var groups = new List>(); - - for (int index = 0; index < soldiers.Count; index++) - { - var soldier = soldiers[index]; - - if (!alreadyGrouped.Contains(soldier)) - { - var group = new List(); - GetNearChain(ref group, soldiers, soldier, maxRange); - alreadyGrouped.AddRange(group); - - if (group.Count > 0) - groups.Add(group); - } - } - - return groups; - } - - // Get all soldiers who are close to each other in a chain - public void GetNearChain(ref List group, List soldiers, Soldier soldier, float maxRange) - { - for (int index = 0; index < soldiers.Count; index++) - { - var nearSoldier = soldiers[index]; - - if (!group.Contains(nearSoldier)) - { - float distance = Vector2.Distance(nearSoldier.Transform.World + nearSoldier.Transform.World.Origin, - soldier.Transform.World + soldier.Transform.World.Origin); - if (distance <= maxRange) - { - group.Add(nearSoldier); - GetNearChain(ref group, soldiers, nearSoldier, maxRange); - } - } - } - } - - private DynamicGroupManager groupManager; - - public void Update(GameTime gameTime) - { - KeyboardState ks = Keyboard.GetState(); - - // Brightness - if (ks.IsKeyUp(Keys.Up) && lastKeyboardState.IsKeyDown(Keys.Up)) - core.Brightness = (int)MathHelper.Clamp(core.Brightness + 5, 0, 254); - if (ks.IsKeyUp(Keys.Down) && lastKeyboardState.IsKeyDown(Keys.Down)) - core.Brightness = (int)MathHelper.Clamp(core.Brightness - 5, 0, 254); - - // Debug test: grouping - if (ks.IsKeyUp(Keys.K) && lastKeyboardState.IsKeyDown(Keys.K)) - { - if (groupManager == null) - groupManager = new DynamicGroupManager(core.Squad.Cast().ToList(), Grid.TileSize * 1.5f); - groupManager.UpdateGroups(); - //var groups = GetGroups(core.Squad, (float)Grid.TileSize * 1.5f); - var groups = groupManager.Groups; - foreach (var group in groups) - { - Debug.WriteLine("Group #" + groups.IndexOf(group) + " Panic: " + group.Panic); - foreach (var entity in group.Entities) - { - var soldier = (Soldier)entity; - Debug.WriteLine(soldier.Name + " " + soldier.GetHashCode()); - } - } - } - - // Next turn - if (ks.IsKeyDown(Keys.Enter) && lastKeyboardState.IsKeyUp(Keys.Enter)) - { - if (core.TurnManager.CurrentTurn == core.TurnManager["Player"]) - { - foreach (var unit in core.Squad) - { - unit.step = 0; - unit.Fired = false; - } - - core.TurnManager.EndTurn(); - } - - if (core.TurnManager.CurrentTurn == core.TurnManager["Computer"]) - { - core.TestMonster.step = 0; - Point target = Point.Zero; - float distance = 7000; - foreach (var unit in core.Squad) - { - Vector2 soldier = new Vector2(unit.Transform.Grid.X, unit.Transform.Grid.Y); - Vector2 monster = new Vector2(core.TestMonster.Transform.Grid.X, core.TestMonster.Transform.Grid.Y); - - float newDistance = Vector2.Distance(soldier, monster); - - if (newDistance < distance) - { - target = unit.Transform.Grid; - distance = newDistance; - } - } - core.TestMonster.GetComponent().Goal = target; - distance = 7000; - - core.TurnManager.EndTurn(); - } - } - if (ks.IsKeyDown(Keys.A)) - { - /*foreach (var unit in core.Swarm) - { - unit.X = 200; - unit.Y = 200; - }*/ - core.TestMonster.X = 12; - core.TestMonster.Y = 4; - } - if (ks.IsKeyDown(Keys.N)) - { - - } - if (ks.IsKeyDown(Keys.S)) - { - foreach (var soldier in core.Squad) - { - if (soldier.Selected == true && soldier.Fired == false) - { - core.Volley.Add(new Shot(core.EntityManager, soldier.GetComponent())); - soldier.Fired = true; - foreach (var shot in core.Volley) - { - if (shot.select == false) - { - shot.select = true; - shot.X = soldier.X; - shot.Y = soldier.Y; - //shot.Transform.World.X += Grid.TileSize / 2; - //shot.Transform.World.Y += Grid.TileSize / 2; - } - if (shot.direction == Vector2.Zero) - { - double directionX = Math.Cos(soldier.GetComponent().World.Rotation); - double directionY = Math.Sin(soldier.GetComponent().World.Rotation); - shot.direction = new Vector2((float)directionX, (float)directionY); - shot.Transform.World.Rotation = soldier.Transform.World.Rotation; - } - } - } - } - } - if (ks.IsKeyDown(Keys.C)) - { - foreach (var soldier in core.Squad) - { - if (soldier.Selected == true && soldier.Fired == false) - { - Point checker = Point.Zero; - double directionX = Math.Cos(soldier.GetComponent().World.Rotation); - double directionY = Math.Sin(soldier.GetComponent().World.Rotation); - Vector2 direction = new Vector2((int)directionX, (int)directionY); - int range = 120; - int current = 0; - bool hit = false; - checker = new Point((int)soldier.Transform.World.Origin.X, (int)soldier.Transform.World.Origin.Y); - do - { - checker.X += (int)direction.X * checkerSpeed; - checker.Y += (int)direction.Y * checkerSpeed; - foreach (var creature in core.Swarm) - { - if (creature.GetComponent().Rectangle.Contains(checker)) - { - hit = true; - } - } - if (core.TestMonster.GetComponent().Rectangle.Contains(checker)) - { - - } - current++; - } while (hit == false && current < range); - current = 0; - if (soldier.Selected == true && soldier.Fired == false && hit == true) - { - core.Volley.Add(new Shot(core.EntityManager, soldier.GetComponent())); - soldier.Fired = true; - foreach (var shot in core.Volley) - { - if (shot.select == false) - { - shot.select = true; - shot.X = soldier.X; - shot.Y = soldier.Y; - //shot.Transform.World.X += Grid.TileSize / 2; - //shot.Transform.World.Y += Grid.TileSize / 2; - } - if (shot.direction == Vector2.Zero) - { - shot.direction = new Vector2((float)directionX, (float)directionY); - shot.Transform.World.Rotation = soldier.Transform.World.Rotation; - } - } - } - hit = false; - } - } - } - - lastKeyboardState = ks; - } - } +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using Microsoft.Xna.Framework.Media; + +namespace DepthsBelow +{ + public class KeyboardInput + { + Core core; + private KeyboardState lastKeyboardState; + + int checkerSpeed = 2; + + public KeyboardInput(Core core) + { + this.core = core; + } + + /*public List> GetGroups(List soldiers) + { + var ungrouped = soldiers.ToList(); + var groups = new List>(); + + for (int index = 0; index < soldiers.Count; index++) + { + var checkingSoldier = soldiers[index]; + var group = new List(); + + for (int i = 0; i < soldiers.Count; i++) + { + var soldier = soldiers[i]; + if (ungrouped.Contains(soldier)) + { + float distance = Vector2.Distance(checkingSoldier.Transform.World + checkingSoldier.Transform.World.Origin, + soldier.Transform.World + soldier.Transform.World.Origin); + if (distance <= ((float) Grid.TileSize * 1f)) + { + ungrouped.Remove(soldier); + group.Add(soldier); + } + } + } + + if (group.Count > 0) + groups.Add(group); + } + + return groups; + }*/ + + // Get a list of dynamic soldier groups + public List> GetGroups(List soldiers, float maxRange) + { + var alreadyGrouped = new List(); + var groups = new List>(); + + for (int index = 0; index < soldiers.Count; index++) + { + var soldier = soldiers[index]; + + if (!alreadyGrouped.Contains(soldier)) + { + var group = new List(); + GetNearChain(ref group, soldiers, soldier, maxRange); + alreadyGrouped.AddRange(group); + + if (group.Count > 0) + groups.Add(group); + } + } + + return groups; + } + + // Get all soldiers who are close to each other in a chain + public void GetNearChain(ref List group, List soldiers, Soldier soldier, float maxRange) + { + for (int index = 0; index < soldiers.Count; index++) + { + var nearSoldier = soldiers[index]; + + if (!group.Contains(nearSoldier)) + { + float distance = Vector2.Distance(nearSoldier.Transform.World + nearSoldier.Transform.World.Origin, + soldier.Transform.World + soldier.Transform.World.Origin); + if (distance <= maxRange) + { + group.Add(nearSoldier); + GetNearChain(ref group, soldiers, nearSoldier, maxRange); + } + } + } + } + + private DynamicGroupManager groupManager; + + public void Update(GameTime gameTime) + { + KeyboardState ks = Keyboard.GetState(); + + // Brightness + if (ks.IsKeyUp(Keys.Up) && lastKeyboardState.IsKeyDown(Keys.Up)) + core.Brightness = (int)MathHelper.Clamp(core.Brightness + 5, 0, 254); + if (ks.IsKeyUp(Keys.Down) && lastKeyboardState.IsKeyDown(Keys.Down)) + core.Brightness = (int)MathHelper.Clamp(core.Brightness - 5, 0, 254); + + // Debug test: grouping + if (ks.IsKeyUp(Keys.K) && lastKeyboardState.IsKeyDown(Keys.K)) + { + if (groupManager == null) + groupManager = new DynamicGroupManager(core.Squad.Cast().ToList(), Grid.TileSize * 1.5f); + groupManager.UpdateGroups(); + //var groups = GetGroups(core.Squad, (float)Grid.TileSize * 1.5f); + var groups = groupManager.Groups; + foreach (var group in groups) + { + Debug.WriteLine("Group #" + groups.IndexOf(group) + " Panic: " + group.Panic); + foreach (var entity in group.Entities) + { + var soldier = (Soldier)entity; + Debug.WriteLine(soldier.Name + " " + soldier.GetHashCode()); + } + } + } + + // Next turn + if (ks.IsKeyDown(Keys.Enter) && lastKeyboardState.IsKeyUp(Keys.Enter)) + { + if (core.TurnManager.CurrentTurn == core.TurnManager["Player"]) + { + foreach (var unit in core.Squad) + { + unit.step = 0; + unit.Fired = false; + } + + core.TurnManager.EndTurn(); + } + + if (core.TurnManager.CurrentTurn == core.TurnManager["Computer"]) + { + foreach (var enemy in core.Swarm) + { + enemy.step = 0; + Point target = Point.Zero; + float distance = 7000; + foreach (var unit in core.Squad) + { + Vector2 soldier = new Vector2(unit.Transform.Grid.X, unit.Transform.Grid.Y); + Vector2 monster = new Vector2(enemy.Transform.Grid.X, enemy.Transform.Grid.Y); + + float newDistance = Vector2.Distance(soldier, monster); + + if (newDistance < distance) + { + target = unit.Transform.Grid; + distance = newDistance; + } + } + enemy.GetComponent().Goal = target; + distance = 7000; + + } + + core.TestMonster.step = 0; + Point testTarget = Point.Zero; + float testDistance = 7000; + foreach (var unit in core.Squad) + { + Vector2 soldier = new Vector2(unit.Transform.Grid.X, unit.Transform.Grid.Y); + Vector2 monster = new Vector2(core.TestMonster.Transform.Grid.X, core.TestMonster.Transform.Grid.Y); + + float newDistance = Vector2.Distance(soldier, monster); + + if (newDistance < testDistance) + { + testTarget = unit.Transform.Grid; + testDistance = newDistance; + } + } + core.TestMonster.GetComponent().Goal = testTarget; + testDistance = 7000; + + core.TurnManager.EndTurn(); + } + } + if (ks.IsKeyDown(Keys.A)) + { + /*foreach (var unit in core.Swarm) + { + unit.X = 200; + unit.Y = 200; + }*/ + core.TestMonster.X = 12; + core.TestMonster.Y = 4; + } + if (ks.IsKeyDown(Keys.N)) + { + + } + if (ks.IsKeyDown(Keys.S)) + { + foreach (var soldier in core.Squad) + { + if (soldier.Selected == true && soldier.Fired == false) + { + core.Volley.Add(new Shot(core.EntityManager, soldier.GetComponent())); + soldier.Fired = true; + foreach (var shot in core.Volley) + { + if (shot.select == false) + { + shot.select = true; + shot.X = soldier.X; + shot.Y = soldier.Y; + //shot.Transform.World.X += Grid.TileSize / 2; + //shot.Transform.World.Y += Grid.TileSize / 2; + } + if (shot.direction == Vector2.Zero) + { + double directionX = Math.Cos(soldier.GetComponent().World.Rotation); + double directionY = Math.Sin(soldier.GetComponent().World.Rotation); + shot.direction = new Vector2((float)directionX, (float)directionY); + shot.Transform.World.Rotation = soldier.Transform.World.Rotation; + } + } + } + } + } + if (ks.IsKeyDown(Keys.C)) + { + foreach (var soldier in core.Squad) + { + if (soldier.Selected == true && soldier.Fired == false) + { + Point checker = Point.Zero; + double directionX = Math.Cos(soldier.GetComponent().World.Rotation); + double directionY = Math.Sin(soldier.GetComponent().World.Rotation); + Vector2 direction = new Vector2((int)directionX, (int)directionY); + int range = 120; + int current = 0; + bool hit = false; + checker = new Point((int)soldier.Transform.World.Origin.X, (int)soldier.Transform.World.Origin.Y); + do + { + checker.X += (int)direction.X * checkerSpeed; + checker.Y += (int)direction.Y * checkerSpeed; + foreach (var creature in core.Swarm) + { + if (creature.GetComponent().Rectangle.Contains(checker)) + { + hit = true; + } + } + if (core.TestMonster.GetComponent().Rectangle.Contains(checker)) + { + + } + current++; + } while (hit == false && current < range); + current = 0; + if (soldier.Selected == true && soldier.Fired == false && hit == true) + { + core.Volley.Add(new Shot(core.EntityManager, soldier.GetComponent())); + soldier.Fired = true; + foreach (var shot in core.Volley) + { + if (shot.select == false) + { + shot.select = true; + shot.X = soldier.X; + shot.Y = soldier.Y; + //shot.Transform.World.X += Grid.TileSize / 2; + //shot.Transform.World.Y += Grid.TileSize / 2; + } + if (shot.direction == Vector2.Zero) + { + shot.direction = new Vector2((float)directionX, (float)directionY); + shot.Transform.World.Rotation = soldier.Transform.World.Rotation; + } + } + } + hit = false; + } + } + } + + lastKeyboardState = ks; + } + } } \ No newline at end of file diff --git a/DepthsBelow/DepthsBelow/Shot.cs b/DepthsBelow/DepthsBelow/Shot.cs index f8ce89e..743c736 100755 --- a/DepthsBelow/DepthsBelow/Shot.cs +++ b/DepthsBelow/DepthsBelow/Shot.cs @@ -1,74 +1,85 @@ -using System; -using System.Collections.Generic; -using DepthsBelow.Component; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Content; -using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework.Input; - -namespace DepthsBelow -{ - public class Shot : Entity - { - public static Texture2D Texture; - public Color Color; - public static Point Origin; - - Stat soldierStat; - - public Stat Stat - { - get { return soldierStat; } - set { soldierStat = value; } - } - - private bool _selected = false; //I guess as in "currently doing something" - - public bool select - { - get { return _selected; } - set - { - _selected = value; - } - } - - public List Volley; - - public Vector2 direction = Vector2.Zero; - - public Shot(EntityManager entityManager, Stat _soldierStat) - : base(entityManager) - { - if (Texture == null) - LoadContent(); - - Transform.World.Origin = new Vector2(16, 16); - - Stat = _soldierStat; - - 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); - - } - public static void LoadContent() - { - Texture = GameServices.GetService().Load("images/Shot"); - } - public override void Update(GameTime gameTime) - { - base.Update(gameTime); - - float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f; - List soldierCollisions = new List(); - - // HACK: Make this work properly with other speeds... - float speed = 4f; - Transform.World.Position += speed * direction; - } - } +using System; +using System.Collections.Generic; +using DepthsBelow.Component; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; + +namespace DepthsBelow +{ + public class Shot : Entity + { + public static Texture2D Texture; + public Color Color; + public static Point Origin; + + Stat soldierStat; + + public Stat Stat + { + get { return soldierStat; } + set { soldierStat = value; } + } + + private bool _selected = false; //I guess as in "currently doing something" + + public bool select + { + get { return _selected; } + set + { + _selected = value; + } + } + + public List Volley; + + public Vector2 direction = Vector2.Zero; + + public Shot(EntityManager entityManager, Stat _soldierStat) + : base(entityManager) + { + if (Texture == null) + LoadContent(); + + Transform.World.Origin = new Vector2(16, 16); + + Stat = _soldierStat; + + 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); + + } + public static void LoadContent() + { + Texture = GameServices.GetService().Load("images/Shot"); + } + public override void Update(GameTime gameTime) + { + base.Update(gameTime); + + float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f; + List soldierCollisions = new List(); + + // HACK: Make this work properly with other speeds... + float speed = 4f; + Transform.World.Position += speed * direction; + + foreach (var entity in entityManager.Entities) + { + if (entity is SmallEnemy) + { + if (this.GetComponent().Rectangle.Intersects(entity.GetComponent().Rectangle)) + { + + } + } + } + } + } } \ No newline at end of file diff --git a/DepthsBelow/DepthsBelow/Utility.cs b/DepthsBelow/DepthsBelow/Utility.cs index e37b1b2..0aaf88d 100755 --- a/DepthsBelow/DepthsBelow/Utility.cs +++ b/DepthsBelow/DepthsBelow/Utility.cs @@ -1,107 +1,107 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using DepthsBelow.Component; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; - -namespace DepthsBelow -{ - /// - /// Various global utility functions. - /// - static class Utility - { - /// - /// Calculate the chance of an entity with a Stat component to hit another unit. - /// - /// The attacking unit. - /// The recieving unit. - /// Returns a hit chance percentage. - public static int CalculateHitChance(Entity attacker, Entity defender) - { - Stat shooting = attacker.GetComponent(); - Stat dodging = defender.GetComponent(); - if (shooting == null || dodging == null) - { - return 0; - } - int baseHitChance = shooting.GetAim; - int baseDodgeChance = dodging.GetDodge; - int distance = (int)Vector2.Distance(attacker.GetComponent().World.Position, defender.GetComponent().World.Position); - int basePenalty = shooting.Penalty(distance / (Grid.TileSize)); - Console.WriteLine("Penalty = " + basePenalty); - int chanceToHit = baseHitChance - baseDodgeChance - basePenalty; - //Console.WriteLine(chanceToHit); - 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; - }*/ - private static Texture2D blank; - - public static void DrawLine(SpriteBatch batch, Color color, float width, Vector2 start, Vector2 end) - { - if (blank == null) - { - blank = new Texture2D(Core.GraphicsDeviceManager.GraphicsDevice, 1, 1, false, SurfaceFormat.Color); - blank.SetData(new[] { color }); - } - - float angle = (float)Math.Atan2(end.Y - start.Y, end.X - start.X); - float length = Vector2.Distance(start, end); - - batch.Draw(blank, start, null, color, - angle, Vector2.Zero, new Vector2(length, width), - SpriteEffects.None, 0); - } - - private static Texture2D solidTexture = null; - /// - /// Creates a 1x1 solid white XNA texture. - /// - /// Returns a 1x1 solid white Texture2D object. - public static Texture2D GetSolidTexture() - { - if (solidTexture == null) - { - solidTexture = new Texture2D(GameServices.GetService(), 1, 1); - solidTexture.SetData(new Color[] { Color.White }); - } - - return solidTexture; - } - - /// - /// Converts rectangles with negative sizes to positive sizes with its position offset. - /// - /// The rectangle with potentially negative sizes. - /// Returns a rectangle with positive sizes. - public static Rectangle MakeRectanglePositive(Rectangle rect) - { - if (rect.Width < 0) - { - rect.X += rect.Width; - rect.Width = -rect.Width; - } - - if (rect.Height < 0) - { - rect.Y += rect.Height; - rect.Height = -rect.Height; - } - - return rect; - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using DepthsBelow.Component; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace DepthsBelow +{ + /// + /// Various global utility functions. + /// + static class Utility + { + /// + /// Calculate the chance of an entity with a Stat component to hit another unit. + /// + /// The attacking unit. + /// The recieving unit. + /// Returns a hit chance percentage. + public static int CalculateHitChance(Entity attacker, Entity defender) + { + Stat shooting = attacker.GetComponent(); + Stat dodging = defender.GetComponent(); + if (shooting == null || dodging == null) + { + return 0; + } + int baseHitChance = shooting.GetAim; + int baseDodgeChance = dodging.GetDodge; + int distance = (int)Vector2.Distance(attacker.GetComponent().World.Position, defender.GetComponent().World.Position); + int basePenalty = shooting.Penalty(distance / (Grid.TileSize)); + Console.WriteLine("Penalty = " + basePenalty); + int chanceToHit = baseHitChance - baseDodgeChance - basePenalty; + //Console.WriteLine(chanceToHit); + 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; + } + private static Texture2D blank; + + public static void DrawLine(SpriteBatch batch, Color color, float width, Vector2 start, Vector2 end) + { + if (blank == null) + { + blank = new Texture2D(Core.GraphicsDeviceManager.GraphicsDevice, 1, 1, false, SurfaceFormat.Color); + blank.SetData(new[] { color }); + } + + float angle = (float)Math.Atan2(end.Y - start.Y, end.X - start.X); + float length = Vector2.Distance(start, end); + + batch.Draw(blank, start, null, color, + angle, Vector2.Zero, new Vector2(length, width), + SpriteEffects.None, 0); + } + + private static Texture2D solidTexture = null; + /// + /// Creates a 1x1 solid white XNA texture. + /// + /// Returns a 1x1 solid white Texture2D object. + public static Texture2D GetSolidTexture() + { + if (solidTexture == null) + { + solidTexture = new Texture2D(GameServices.GetService(), 1, 1); + solidTexture.SetData(new Color[] { Color.White }); + } + + return solidTexture; + } + + /// + /// Converts rectangles with negative sizes to positive sizes with its position offset. + /// + /// The rectangle with potentially negative sizes. + /// Returns a rectangle with positive sizes. + public static Rectangle MakeRectanglePositive(Rectangle rect) + { + if (rect.Width < 0) + { + rect.X += rect.Width; + rect.Width = -rect.Width; + } + + if (rect.Height < 0) + { + rect.Y += rect.Height; + rect.Height = -rect.Height; + } + + return rect; + } + } +}