From 4f8a5ccdb2e56eab81cb569b8ad060f5c7a4ea99 Mon Sep 17 00:00:00 2001 From: Niklas Ulf Anders Cullberg Date: Wed, 17 Oct 2012 10:52:05 +0200 Subject: [PATCH] Added Utility, Stat, removed FindDistance(Core) --- .../Component/{Shooting.cs => Stat.cs} | 30 +- DepthsBelow/DepthsBelow/Core.cs | 414 +++++++++--------- DepthsBelow/DepthsBelow/DepthsBelow.csproj | 403 ++++++++--------- DepthsBelow/DepthsBelow/Entity.cs | 143 +++--- DepthsBelow/DepthsBelow/KeyboardInput.cs | 7 +- DepthsBelow/DepthsBelow/Utility.cs | 26 ++ 6 files changed, 524 insertions(+), 499 deletions(-) rename DepthsBelow/DepthsBelow/Component/{Shooting.cs => Stat.cs} (70%) create mode 100644 DepthsBelow/DepthsBelow/Utility.cs diff --git a/DepthsBelow/DepthsBelow/Component/Shooting.cs b/DepthsBelow/DepthsBelow/Component/Stat.cs similarity index 70% rename from DepthsBelow/DepthsBelow/Component/Shooting.cs rename to DepthsBelow/DepthsBelow/Component/Stat.cs index 382d5ac..2a00b2a 100644 --- a/DepthsBelow/DepthsBelow/Component/Shooting.cs +++ b/DepthsBelow/DepthsBelow/Component/Stat.cs @@ -5,7 +5,7 @@ using System.Text; namespace DepthsBelow.Component { - class Shooting : Component + public class Stat : Component { protected int stepsTaken = 0; protected int distanceToTarget = 0; @@ -16,7 +16,22 @@ namespace DepthsBelow.Component protected int penaltyRange = 5; - public Shooting(Entity parent) : base (parent) + public int GetAim() + { + return (soldierAim + weaponAccuracy); + } + + public int GetDodge() + { + return enemyDodge; + } + + public int Penalty(int distance) + { + return panic + GetPenalty(stepsTaken) + (int)(GetPenalty(distance) / 2); + } + + public Stat(Entity parent) : base (parent) { } @@ -26,16 +41,7 @@ namespace DepthsBelow.Component return false; } - public int CalculateChance() - { - int baseHitChance = soldierAim + weaponAccuracy; - int baseDodgeChance = panic + enemyDodge; - int basePenalty = ReturnPenalty(stepsTaken) + (int)(ReturnPenalty(distanceToTarget) / 2); - int chanceToHit = baseHitChance - baseDodgeChance - basePenalty; - return chanceToHit; - } - - public int ReturnPenalty(int distance) + public int GetPenalty(int distance) { int penalty = 0; if (distance >= penaltyRange * 3) diff --git a/DepthsBelow/DepthsBelow/Core.cs b/DepthsBelow/DepthsBelow/Core.cs index cbe8091..7cea473 100755 --- a/DepthsBelow/DepthsBelow/Core.cs +++ b/DepthsBelow/DepthsBelow/Core.cs @@ -1,213 +1,201 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using DepthsBelow.GUI; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Audio; -using Microsoft.Xna.Framework.Content; -using Microsoft.Xna.Framework.GamerServices; -using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework.Input; -using Microsoft.Xna.Framework.Media; - -namespace DepthsBelow -{ - /// - /// This is the main type for your game - /// - public class Core : Microsoft.Xna.Framework.Game - { - public static GraphicsDeviceManager GraphicsDeviceManager; - SpriteBatch spriteBatch; - - public Lua Lua; - - public Camera Camera; - - public bool PlayerTurn = true; - public static MouseInput MouseInput; - public static KeyboardInput KeyboardInput; - public List Squad; - public List Swarm; - public List Volley; - public static Map Map; - - public SmallEnemy TestMonster; - - public Core() - { - GraphicsDeviceManager = new GraphicsDeviceManager(this); - Content.RootDirectory = "Content"; - - GraphicsDeviceManager.PreferredBackBufferWidth = 1280; - GraphicsDeviceManager.PreferredBackBufferHeight = 720; - GraphicsDeviceManager.IsFullScreen = false; - - //graphics.SynchronizeWithVerticalRetrace = false; - this.IsFixedTimeStep = false; - GraphicsDeviceManager.ApplyChanges(); - - this.IsMouseVisible = true; - - GameServices.AddService(GraphicsDevice); - GameServices.AddService(Content); - } - - /// - /// Allows the game to perform any initialization it needs to before starting to run. - /// This is where it can query for any required services and load any non-graphic - /// related content. Calling base.Initialize will enumerate through any components - /// and initialize them as well. - /// - protected override void Initialize() - { - // TODO: Add your initialization logic here - Camera = new Camera(this); - Squad = new List(); - Swarm = new List(); - Volley = new List(); - - TestMonster = new SmallEnemy(this, ref Swarm); - - // Run scripts after everything is initialized - Lua = new Lua(); - Lua.LoadScripts(); - - base.Initialize(); - } - - /// - /// LoadContent will be called once per game and is the place to load - /// all of your content. - /// - protected override void LoadContent() - { - // Create a new SpriteBatch, which can be used to draw textures. - spriteBatch = new SpriteBatch(GraphicsDevice); - - Map = Content.Load("maps/Cave.Level1"); - // Load map objects - Map.ParseObjects(this); - - // TODO: use this.Content to load your game content here - Soldier.LoadContent(this); - SmallEnemy.LoadContent(this); - Shot.LoadContent(this); - - MouseInput = new MouseInput(this); - MouseInput.LoadContent(); - KeyboardInput = new KeyboardInput(this); - - TestMonster.X = 12; - TestMonster.Y = 4; - - //frame.Add(text); - } - - /// - /// UnloadContent will be called once per game and is the place to unload - /// all content. - /// - protected override void UnloadContent() - { - // TODO: Unload any non ContentManager content here - } - - /// - /// Allows the game to run logic such as updating the world, - /// checking for collisions, gathering input, and playing audio. - /// - /// Provides a snapshot of timing values. - protected override void Update(GameTime gameTime) - { - // Allows the game to exit - if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) - this.Exit(); - - Camera.Update(gameTime); - MouseInput.Update(gameTime); - KeyboardInput.Update(gameTime); - - // Update soldiers in squad - foreach (var soldier in Squad) - soldier.Update(gameTime); - - foreach (var body in Swarm) - body.Update(gameTime); - - foreach (var bullet in Volley) - bullet.Update(gameTime); - - TestMonster.Update(gameTime); - GUIManager.Update(gameTime); - - base.Update(gameTime); - } - - public int FindDistance(Vector2 target, Vector2 start) - { - Vector2 combine = new Vector2(target.X - start.X, target.Y - start.Y); - - int result = 0; - int firstRestult = (int)Math.Sqrt((int)combine.X^2 + (int)combine.Y^2); - - result = (int)Vector2.Distance(target, start); - - return result; - } - - /// - /// This is called when the game should draw itself. - /// - /// Provides a snapshot of timing values. - protected override void Draw(GameTime gameTime) - { - GraphicsDevice.Clear(Color.Black); - - // Start drawing using the Camera transform - spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, - Camera.Transform); - - // Draw the level - Map.Draw(spriteBatch); - - // Draw units - foreach (var soldier in Squad) - { - var sr = soldier.GetComponent(); - if (sr != null) - sr.Draw(spriteBatch); - } - foreach (var body in Swarm) - { - var sr = body.GetComponent(); - if (sr != null) - sr.Draw(spriteBatch); - } - foreach (var bullet in Volley) - { - var sr = bullet.GetComponent(); - if (sr != null) - sr.Draw(spriteBatch); - } - - // Draw mouse input visuals - MouseInput.Draw(spriteBatch); - - TestMonster.GetComponent().Draw(spriteBatch); - - spriteBatch.End(); - - // Start drawing GUI components - spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.AnisotropicClamp, null, null, null, Matrix.Identity); - GUIManager.Draw(spriteBatch); - spriteBatch.End(); - - base.Draw(gameTime); - } - - - } -} +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using DepthsBelow.GUI; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Audio; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.GamerServices; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using Microsoft.Xna.Framework.Media; + +namespace DepthsBelow +{ + /// + /// This is the main type for your game + /// + public class Core : Microsoft.Xna.Framework.Game + { + public static GraphicsDeviceManager GraphicsDeviceManager; + SpriteBatch spriteBatch; + + public Lua Lua; + + public Camera Camera; + + public bool PlayerTurn = true; + public static MouseInput MouseInput; + public static KeyboardInput KeyboardInput; + public List Squad; + public List Swarm; + public List Volley; + public static Map Map; + + public SmallEnemy TestMonster; + + public Core() + { + GraphicsDeviceManager = new GraphicsDeviceManager(this); + Content.RootDirectory = "Content"; + + GraphicsDeviceManager.PreferredBackBufferWidth = 1280; + GraphicsDeviceManager.PreferredBackBufferHeight = 720; + GraphicsDeviceManager.IsFullScreen = false; + + //graphics.SynchronizeWithVerticalRetrace = false; + this.IsFixedTimeStep = false; + GraphicsDeviceManager.ApplyChanges(); + + this.IsMouseVisible = true; + + GameServices.AddService(GraphicsDevice); + GameServices.AddService(Content); + } + + /// + /// Allows the game to perform any initialization it needs to before starting to run. + /// This is where it can query for any required services and load any non-graphic + /// related content. Calling base.Initialize will enumerate through any components + /// and initialize them as well. + /// + protected override void Initialize() + { + // TODO: Add your initialization logic here + Camera = new Camera(this); + Squad = new List(); + Swarm = new List(); + Volley = new List(); + + TestMonster = new SmallEnemy(this, ref Swarm); + + // Run scripts after everything is initialized + Lua = new Lua(); + Lua.LoadScripts(); + + base.Initialize(); + } + + /// + /// LoadContent will be called once per game and is the place to load + /// all of your content. + /// + protected override void LoadContent() + { + // Create a new SpriteBatch, which can be used to draw textures. + spriteBatch = new SpriteBatch(GraphicsDevice); + + Map = Content.Load("maps/Cave.Level1"); + // Load map objects + Map.ParseObjects(this); + + // TODO: use this.Content to load your game content here + Soldier.LoadContent(this); + SmallEnemy.LoadContent(this); + Shot.LoadContent(this); + + MouseInput = new MouseInput(this); + MouseInput.LoadContent(); + KeyboardInput = new KeyboardInput(this); + + TestMonster.X = 12; + TestMonster.Y = 4; + + //frame.Add(text); + } + + /// + /// UnloadContent will be called once per game and is the place to unload + /// all content. + /// + protected override void UnloadContent() + { + // TODO: Unload any non ContentManager content here + } + + /// + /// Allows the game to run logic such as updating the world, + /// checking for collisions, gathering input, and playing audio. + /// + /// Provides a snapshot of timing values. + protected override void Update(GameTime gameTime) + { + // Allows the game to exit + if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) + this.Exit(); + + Camera.Update(gameTime); + MouseInput.Update(gameTime); + KeyboardInput.Update(gameTime); + + // Update soldiers in squad + foreach (var soldier in Squad) + soldier.Update(gameTime); + + foreach (var body in Swarm) + body.Update(gameTime); + + foreach (var bullet in Volley) + bullet.Update(gameTime); + + TestMonster.Update(gameTime); + GUIManager.Update(gameTime); + + base.Update(gameTime); + } + + /// + /// This is called when the game should draw itself. + /// + /// Provides a snapshot of timing values. + protected override void Draw(GameTime gameTime) + { + GraphicsDevice.Clear(Color.Black); + + // Start drawing using the Camera transform + spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, + Camera.Transform); + + // Draw the level + Map.Draw(spriteBatch); + + // Draw units + foreach (var soldier in Squad) + { + var sr = soldier.GetComponent(); + if (sr != null) + sr.Draw(spriteBatch); + } + foreach (var body in Swarm) + { + var sr = body.GetComponent(); + if (sr != null) + sr.Draw(spriteBatch); + } + foreach (var bullet in Volley) + { + var sr = bullet.GetComponent(); + if (sr != null) + sr.Draw(spriteBatch); + } + + // Draw mouse input visuals + MouseInput.Draw(spriteBatch); + + TestMonster.GetComponent().Draw(spriteBatch); + + spriteBatch.End(); + + // Start drawing GUI components + spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.AnisotropicClamp, null, null, null, Matrix.Identity); + GUIManager.Draw(spriteBatch); + spriteBatch.End(); + + base.Draw(gameTime); + } + + + } +} diff --git a/DepthsBelow/DepthsBelow/DepthsBelow.csproj b/DepthsBelow/DepthsBelow/DepthsBelow.csproj index a11ea38..8a968e4 100644 --- a/DepthsBelow/DepthsBelow/DepthsBelow.csproj +++ b/DepthsBelow/DepthsBelow/DepthsBelow.csproj @@ -1,203 +1,204 @@ - - - - {94683C5B-052D-4143-96D8-35C67E831000} - {6D335F3A-9D43-41b4-9D22-F6F17C4BE596};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - Debug - x86 - WinExe - Properties - DepthsBelow - DepthsBelow - v4.0 - Client - v4.0 - Windows - HiDef - faa0577e-a03e-43cf-89c7-0a03f4c22717 - Game - Game.ico - GameThumbnail.png - false - publish\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 1 - 1.0.0.%2a - false - true - true - - - true - full - false - bin\x86\Debug - DEBUG;TRACE;WINDOWS - prompt - 4 - true - false - x86 - false - - - pdbonly - true - bin\x86\Release - TRACE;WINDOWS - prompt - 4 - true - false - x86 - true - - - ECE66DEF071576E8D2F7D57232C0569623E3E2DF - - - DepthsBelow_TemporaryKey.pfx - - - true - - - true - - - - ..\..\LuaInterface\LuaInterface.dll - False - - - False - - - False - - - False - - - False - - - False - - - False - - - False - - - False - - - False - - - False - - - False - - - - False - - - False - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {CD3F949D-54AA-4D38-99DB-92905A375D84} - AStar - - - DepthsBelowContent %28Content%29 - Content - {433A77A2-DE52-4875-A4EF-232CF59C2DD3} - - - - - False - Microsoft .NET Framework 4 Client Profile %28x86 and x64%29 - true - - - False - .NET Framework 3.5 SP1 Client Profile - false - - - False - .NET Framework 3.5 SP1 - false - - - False - Windows Installer 3.1 - true - - - False - Microsoft XNA Framework Redistributable 4.0 - true - - - - - - - - - + + + + {94683C5B-052D-4143-96D8-35C67E831000} + {6D335F3A-9D43-41b4-9D22-F6F17C4BE596};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Debug + x86 + WinExe + Properties + DepthsBelow + DepthsBelow + v4.0 + Client + v4.0 + Windows + HiDef + faa0577e-a03e-43cf-89c7-0a03f4c22717 + Game + Game.ico + GameThumbnail.png + false + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 1 + 1.0.0.%2a + false + true + true + + + true + full + false + bin\x86\Debug + DEBUG;TRACE;WINDOWS + prompt + 4 + true + false + x86 + false + + + pdbonly + true + bin\x86\Release + TRACE;WINDOWS + prompt + 4 + true + false + x86 + true + + + ECE66DEF071576E8D2F7D57232C0569623E3E2DF + + + DepthsBelow_TemporaryKey.pfx + + + true + + + true + + + + ..\..\LuaInterface\LuaInterface.dll + False + + + False + + + False + + + False + + + False + + + False + + + False + + + False + + + False + + + False + + + False + + + False + + + + False + + + False + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {CD3F949D-54AA-4D38-99DB-92905A375D84} + AStar + + + DepthsBelowContent %28Content%29 + Content + {433A77A2-DE52-4875-A4EF-232CF59C2DD3} + + + + + False + Microsoft .NET Framework 4 Client Profile %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + false + + + False + Windows Installer 3.1 + true + + + False + Microsoft XNA Framework Redistributable 4.0 + true + + + + + + + + + + --> \ No newline at end of file diff --git a/DepthsBelow/DepthsBelow/Entity.cs b/DepthsBelow/DepthsBelow/Entity.cs index 838dc97..572d108 100644 --- a/DepthsBelow/DepthsBelow/Entity.cs +++ b/DepthsBelow/DepthsBelow/Entity.cs @@ -1,70 +1,73 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Microsoft.Xna.Framework; - -namespace DepthsBelow -{ - public class Entity - { - List Components; - public Component.Transform Transform; - public Component.PixelTransform pixelTransform; - public Component.GridTransform gridTransform; - public int X - { - get { return this.Transform.Grid.X; } - set - { - this.Transform.Grid.X = value; - } - } - public int Y - { - get { return this.Transform.Grid.Y; } - set - { - this.Transform.Grid.Y = value; - } - } - - protected Core core; - - public Entity(Core core) - { - this.core = core; - - Components = new List(); - Transform = new Component.Transform(this); - AddComponent(Transform); - pixelTransform = new Component.PixelTransform(this); - AddComponent(pixelTransform); - gridTransform = new Component.GridTransform(this); - AddComponent(gridTransform); - } - - public T GetComponent() where T : Component.Component - { - foreach (Component.Component c in Components) - { - if (c is T) - return (T)c; - } - - return null; - } - - public void AddComponent(Component.Component c) - { - Components.Add(c); - } - - public virtual void Update(GameTime gameTime) - { - // Update components - foreach (Component.Component c in Components) - c.Update(gameTime); - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework; + +namespace DepthsBelow +{ + public class Entity + { + List Components; + public Component.Transform Transform; + public Component.PixelTransform pixelTransform; + public Component.GridTransform gridTransform; + public Component.Stat stat; + public int X + { + get { return this.Transform.Grid.X; } + set + { + this.Transform.Grid.X = value; + } + } + public int Y + { + get { return this.Transform.Grid.Y; } + set + { + this.Transform.Grid.Y = value; + } + } + + protected Core core; + + public Entity(Core core) + { + this.core = core; + + Components = new List(); + Transform = new Component.Transform(this); + AddComponent(Transform); + pixelTransform = new Component.PixelTransform(this); + AddComponent(pixelTransform); + gridTransform = new Component.GridTransform(this); + AddComponent(gridTransform); + stat = new Component.Stat(this); + AddComponent(stat); + } + + public T GetComponent() where T : Component.Component + { + foreach (Component.Component c in Components) + { + if (c is T) + return (T)c; + } + + return null; + } + + public void AddComponent(Component.Component c) + { + Components.Add(c); + } + + public virtual void Update(GameTime gameTime) + { + // Update components + foreach (Component.Component c in Components) + c.Update(gameTime); + } + } +} diff --git a/DepthsBelow/DepthsBelow/KeyboardInput.cs b/DepthsBelow/DepthsBelow/KeyboardInput.cs index c9dd67e..b0b2f59 100644 --- a/DepthsBelow/DepthsBelow/KeyboardInput.cs +++ b/DepthsBelow/DepthsBelow/KeyboardInput.cs @@ -15,7 +15,7 @@ namespace DepthsBelow Core core; bool Enter = false; bool turn; - int checkerSpeed = 1; + int checkerSpeed = 2; public KeyboardInput(Core core) { @@ -48,13 +48,14 @@ namespace DepthsBelow core.TestMonster.step = 0; core.PlayerTurn = false; Point target = Point.Zero; - int distance = 7000; + 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); - int newDistance = core.FindDistance(soldier, monster); + float newDistance = Vector2.Distance(soldier, monster); + if (newDistance < distance) { target = unit.Transform.Grid; diff --git a/DepthsBelow/DepthsBelow/Utility.cs b/DepthsBelow/DepthsBelow/Utility.cs new file mode 100644 index 0000000..0f0e713 --- /dev/null +++ b/DepthsBelow/DepthsBelow/Utility.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework; + +namespace DepthsBelow +{ + static class Utility + { + public static int CalculateHitChance(Entity attacker, Entity defender) + { + Component.Stat shooting = attacker.GetComponent(); + Component.Stat dodging = defender.GetComponent(); + if (shooting == null || dodging == null) + { + return 0; + } + 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; + } + } +}