diff --git a/DepthsBelow/DepthsBelow/Core.cs b/DepthsBelow/DepthsBelow/Core.cs index 36b7dcf..237359a 100755 --- a/DepthsBelow/DepthsBelow/Core.cs +++ b/DepthsBelow/DepthsBelow/Core.cs @@ -19,33 +19,33 @@ namespace DepthsBelow /// public class Core : Microsoft.Xna.Framework.Game { - public static GraphicsDeviceManager GraphicsDeviceManager; - private SpriteBatch spriteBatch; - private Effect lightShader; - private RenderTarget2D lightRenderTarget; + public static GraphicsDeviceManager GraphicsDeviceManager; + private SpriteBatch spriteBatch; + private Effect lightShader; + private RenderTarget2D lightRenderTarget; private RenderTarget2D sceneRenderTarget; - public Lua Lua; - - public EntityManager EntityManager; + public Lua Lua; + + public EntityManager EntityManager; public TurnManager TurnManager; - public Camera Camera; + public Camera Camera; public Interface Interface; public static MouseInput MouseInput; public static KeyboardInput KeyboardInput; - public List Squad; + public List Squad; public DynamicGroupManager GroupManager; public List Swarm; - public List Volley; - + public List Volley; + public List Spawn; public static Map Map; - public SmallEnemy TestMonster; - + public SmallEnemy TestMonster; + public int Brightness = 30; public Core() @@ -70,24 +70,24 @@ namespace DepthsBelow /// and initialize them as well. /// protected override void Initialize() - { - GameServices.AddService(GraphicsDevice); - GameServices.AddService(Content); - - Lua = new Lua(); - - EntityManager = new EntityManager(); - TurnManager = new TurnManager(new string[] { "Player", "Computer" }); - - Camera = new Camera(this); - Interface = new Interface(this); - - Squad = new List(); - Swarm = new List(); - Volley = new List(); - - Spawn = new List(); - + { + GameServices.AddService(GraphicsDevice); + GameServices.AddService(Content); + + Lua = new Lua(); + + EntityManager = new EntityManager(); + TurnManager = new TurnManager(new string[] { "Player", "Computer" }); + + Camera = new Camera(this); + Interface = new Interface(this); + + Squad = new List(); + Swarm = new List(); + Volley = new List(); + + Spawn = new List(); + base.Initialize(); } @@ -98,15 +98,21 @@ namespace DepthsBelow protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. - spriteBatch = new SpriteBatch(GraphicsDevice); - lightShader = Content.Load("shaders/LightEffect"); - lightRenderTarget = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents); + spriteBatch = new SpriteBatch(GraphicsDevice); + lightShader = Content.Load("shaders/LightEffect"); + lightRenderTarget = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents); sceneRenderTarget = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); Map = Content.Load("maps/Aztek.Test"); + //Map = Content.Load("maps/Cave.Level1"); // Load map objects Map.ParseObjects(this); GroupManager = new DynamicGroupManager(Squad.Cast().ToList(), (float)Grid.TileSize * 1.5f); + Squad[0].Name = "Dean H. Jones"; + Squad[1].Name = "Walter L. Verville"; + Squad[2].Name = "Patrick Y. Southerland"; + Squad[3].Name = "Andrew C. Friend"; + Squad[4].Name = "Gary B. Whitley"; Interface.CreateUnitFrames(Squad); // TODO: use this.Content to load your game content here @@ -116,12 +122,12 @@ namespace DepthsBelow MouseInput = new MouseInput(this); MouseInput.LoadContent(); - KeyboardInput = new KeyboardInput(this); - - TestMonster = new SmallEnemy(EntityManager, ref Swarm); - TestMonster.X = 12; + KeyboardInput = new KeyboardInput(this); + + TestMonster = new SmallEnemy(EntityManager, ref Swarm); + TestMonster.X = 12; TestMonster.Y = 4; - Swarm.Add(TestMonster); + Swarm.Add(TestMonster); // Load Lua scripts after everything is created Lua.LoadScripts(); @@ -163,25 +169,25 @@ namespace DepthsBelow /// This is called when the game should draw itself. /// /// Provides a snapshot of timing values. - protected override void Draw(GameTime gameTime) - { - MouseState ms = Mouse.GetState(); + protected override void Draw(GameTime gameTime) + { + MouseState ms = Mouse.GetState(); // Draw to the lighting render target - GraphicsDevice.SetRenderTarget(lightRenderTarget); - GraphicsDevice.Clear(new Color(Brightness, Brightness, Brightness)); - spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive, SamplerState.PointClamp, null, null, null, - Camera.Transform); - - // Draw flashlights - foreach (var light in EntityManager.GetComponents()) + GraphicsDevice.SetRenderTarget(lightRenderTarget); + GraphicsDevice.Clear(new Color(Brightness, Brightness, Brightness)); + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive, SamplerState.PointClamp, null, null, null, + Camera.Transform); + + // Draw flashlights + foreach (var light in EntityManager.GetComponents()) light.Draw(spriteBatch); spriteBatch.End(); GraphicsDevice.SetRenderTarget(null); // Draw the scene to a render target - GraphicsDevice.SetRenderTarget(sceneRenderTarget); + GraphicsDevice.SetRenderTarget(sceneRenderTarget); GraphicsDevice.Clear(Color.Black); // Start drawing using the Camera transform spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, null, null, null, @@ -190,45 +196,45 @@ namespace DepthsBelow // Draw the level Map.Draw(spriteBatch); - // Draw entities - foreach (var spriteRenderer in EntityManager.GetComponents()) - { - // Only render illuminated entities - var position = Camera.WorldToScreen(spriteRenderer.Parent.Transform.World + spriteRenderer.Parent.Transform.World.Origin); - var sourceRect = new Rectangle((int)position.X, (int)position.Y, 1, 1); - if ( - sourceRect.X >= 0 - && sourceRect.Y >= 0 - && sourceRect.X < lightRenderTarget.Width - && sourceRect.Y < lightRenderTarget.Height - ) - { - var lightData = new Color[1]; - lightRenderTarget.GetData(0, sourceRect, lightData, 0, 1); - if (lightData[0] != new Color(Brightness, Brightness, Brightness)) - spriteRenderer.Draw(spriteBatch); - } - } - -#if DEBUG - var debugTexture = new Texture2D(GraphicsDevice, 1, 1); - debugTexture.SetData(new Color[] { Color.White }); - foreach (var collisionComponent in EntityManager.GetComponents()) - { - spriteBatch.Draw(debugTexture, collisionComponent.Rectangle, Color.Red * 0.5f); - } -#endif - + // Draw entities + foreach (var spriteRenderer in EntityManager.GetComponents()) + { + // Only render illuminated entities + var position = Camera.WorldToScreen(spriteRenderer.Parent.Transform.World + spriteRenderer.Parent.Transform.World.Origin); + var sourceRect = new Rectangle((int)position.X, (int)position.Y, 1, 1); + if ( + sourceRect.X >= 0 + && sourceRect.Y >= 0 + && sourceRect.X < lightRenderTarget.Width + && sourceRect.Y < lightRenderTarget.Height + ) + { + var lightData = new Color[1]; + lightRenderTarget.GetData(0, sourceRect, lightData, 0, 1); + if (lightData[0] != new Color(Brightness, Brightness, Brightness)) + spriteRenderer.Draw(spriteBatch); + } + } + +#if DEBUG + var debugTexture = new Texture2D(GraphicsDevice, 1, 1); + debugTexture.SetData(new Color[] { Color.White }); + foreach (var collisionComponent in EntityManager.GetComponents()) + { + spriteBatch.Draw(debugTexture, collisionComponent.Rectangle, Color.Red * 0.5f); + } +#endif + // Draw mouse input visuals MouseInput.Draw(spriteBatch); spriteBatch.End(); GraphicsDevice.SetRenderTarget(null); - // Draw the scene from the render target with the light shader - lightShader.Parameters["LightsTexture"].SetValue(lightRenderTarget); - lightShader.CurrentTechnique.Passes[0].Apply(); - spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.PointClamp, null, null, lightShader, + // Draw the scene from the render target with the light shader + lightShader.Parameters["LightsTexture"].SetValue(lightRenderTarget); + lightShader.CurrentTechnique.Passes[0].Apply(); + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.PointClamp, null, null, lightShader, Matrix.Identity); spriteBatch.Draw(sceneRenderTarget, Vector2.Zero, Color.White); spriteBatch.End(); diff --git a/DepthsBelow/DepthsBelow/Interface.cs b/DepthsBelow/DepthsBelow/Interface.cs index b1de90d..c92bf07 100755 --- a/DepthsBelow/DepthsBelow/Interface.cs +++ b/DepthsBelow/DepthsBelow/Interface.cs @@ -1,510 +1,510 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using DepthsBelow.GUI; -using Microsoft.Xna.Framework; -using System.Diagnostics; -using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework.Input; -using Microsoft.Xna.Framework.Content; - -namespace DepthsBelow -{ - public class Interface - { - Core core; - - /// - /// The master frame covering the whole game screen. Parent of all frames. - /// This frame handles all mouse interaction with the game world - /// - public Frame UIParent; - - public List PanicFrames = new List(); - public List UnitFrames = new List(); - public Dictionary SoldierToFrame = new Dictionary(); - - Random random = new Random(); - - private bool checkingDirection; - private Vector2 directionStart; - - public Interface(Core core) - { - this.core = core; - - UIParent = new Frame(); - UIParent.Width = GameServices.GetService().Viewport.Width; - UIParent.Height = GameServices.GetService().Viewport.Height; - - // Selection rectangle - var selectionFrame = new Frame(UIParent); - selectionFrame.Texture = Utility.GetSolidTexture(); - selectionFrame.Color = Color.Red * 0.5f; - selectionFrame.Visible = false; - UIParent["selectionFrame"] = selectionFrame; - - // OnClick - UIParent.OnClick += delegate(Frame frame, GUIManager.MouseEventArgs args) - { - KeyboardState ks = Keyboard.GetState(); - MouseState ms = args.MouseState; - - var mousePos = new Point(ms.X, ms.Y); - var mouseRectangle = new Rectangle(mousePos.X, mousePos.Y, 1, 1); - var mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(mousePos.X, mousePos.Y)); - var mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1); - - if (args.LeftButton == ButtonState.Pressed) - { - Debug.WriteLine("Left button pressed on UIParent"); - // Deselect all units - /*if (!ks.IsKeyDown(Keys.LeftControl)) - { - foreach (var unit in core.Squad) - unit.Selected = false; - - foreach (var unitFrame in UnitFrames) - unitFrame.Color = Color.Black; - } - - // Select all units in the rectangle - foreach (var soldier in core.Squad) - { - if (mouseWorldRectangle.Intersects(soldier.GetComponent().Rectangle)) - { - soldier.Selected = true; - - foreach (var unitFrame in UnitFrames) - { - if (unitFrame["soldier"] == soldier) - unitFrame.Color = Color.Blue; - } - } - }*/ - } - - - - // Hide the selection rectangle - //selectionRectangle = Rectangle.Empty; - }; - - // OnPress - UIParent.OnPress += delegate(Frame frame, GUIManager.MouseEventArgs args) - { - //KeyboardState ks = Keyboard.GetState(); - MouseState ms = args.MouseState; - var mousePos = new Point(ms.X, ms.Y); - var mouseRectangle = new Rectangle(mousePos.X, mousePos.Y, 1, 1); - var mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(mousePos.X, mousePos.Y)); - var mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1); - - // Show the selection frame - if (args.LeftButton == ButtonState.Pressed) - { - var selectFrame = (GUI.Frame)frame["selectionFrame"]; - if (!selectFrame.Visible) - { - selectFrame.X = mousePos.X; - selectFrame.Y = mousePos.Y; - selectFrame.Visible = true; - } - } - - if (args.RightButton == ButtonState.Pressed) - { - // Unit rotation - foreach (var unit in core.Squad) - { - if (unit.Selected && mouseWorldRectangle.Intersects(unit.GetComponent().Rectangle)) - { - checkingDirection = true; - directionStart = unit.Transform.World + unit.Transform.World.Origin; - } - } - } - }; - - // OnRelease - UIParent.OnRelease += delegate(Frame frame, GUIManager.MouseEventArgs args) - { - KeyboardState ks = Keyboard.GetState(); - MouseState ms = args.MouseState; - var mousePos = new Point(ms.X, ms.Y); - var mouseRectangle = new Rectangle(mousePos.X, mousePos.Y, 1, 1); - var mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(mousePos.X, mousePos.Y)); - var mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1); - - if (args.LeftButton == ButtonState.Pressed) - { - // Deselect all units - if (!ks.IsKeyDown(Keys.LeftControl)) - { - foreach (var unit in core.Squad) - unit.Selected = false; - - foreach (var unitFrame in UnitFrames) - unitFrame.Color = Color.Black; - } - - // Select all soldier units inside the selection rectangle - var selectFrame = (GUI.Frame)frame["selectionFrame"]; - if (selectFrame.Visible) - { - Console.WriteLine("Before: " + selectFrame.AbsoluteRectangle.X + ", " + selectFrame.AbsoluteRectangle.Y + ", " + selectFrame.AbsoluteRectangle.Width + ", " + selectFrame.AbsoluteRectangle.Height); - - var intersectionRectangle = core.Camera.ScreenToWorld(selectFrame.AbsoluteRectangle); - - Console.WriteLine("After: " + intersectionRectangle.X + ", " + intersectionRectangle.Y + ", " + intersectionRectangle.Width + ", " + intersectionRectangle.Height); - - - // Select all units in the rectangle - foreach (var soldier in core.Squad) - { - if (intersectionRectangle.Intersects(soldier.GetComponent().Rectangle)) - { - soldier.Selected = true; - foreach (var unitFrame in UnitFrames) - { - if (unitFrame["soldier"] == soldier) - unitFrame.Color = Color.Blue; - } - } - } - - selectFrame.Visible = false; - } - } - - // Send orders with right click - if (args.RightButton == ButtonState.Pressed) - { - if (checkingDirection) - { - checkingDirection = false; - } - else - { - Debug.WriteLine("Right button pressed on UIParent"); - foreach (var unit in core.Squad) - if (unit.Selected) - unit.GetComponent().Goal = Grid.WorldToGrid(mouseWorldPos); - } - } - }; - - var button = new GUI.Button(UIParent); - button.SetTexture("images/Enter"); - button.X = 200; - button.Y = 100; - button.Width = 55; - button.Height = 40; - - // Test GUI frames - /*var frame = new GUI.Frame() - { - Y = 100 - }; - - var button = new GUI.Button(frame) - { - Width = 48, - Height = 23, - Texture = core.Content.Load("images/Enter"), - OnClick = delegate(Point clickPos) - { - Debug.WriteLine("That ugly button was clicked at (" + clickPos.X + "," + clickPos.Y + ")"); - } - }; - - var text = new GUI.Text(frame); - text.SetFont("Arial"); - text.Value = "Hello world!";*/ - - // Create some unit frames - /*var panicFrame = CreatePanicFrame(); - var unit1 = CreateUnitFrame("Flight captain Rainbow"); - unit1.X = panicFrame.X; - unit1.Y = panicFrame.Y + panicFrame.Height; - unit1.OnClick += delegate(Frame frame, Frame.MouseEventArgs args) - { - frame.Color = Color.Red; - }; - var unit2 = CreateUnitFrame("Mr. Sparkle"); - unit2.X = unit1.X; - unit2.Y = unit1.Y + unit1.Height; - unit2.OnClick += delegate(Frame frame, Frame.MouseEventArgs args) - { - frame.Color = Color.Red; - };*/ - - /*var frame1 = new GUI.Frame(); - frame1.Layer = 0; - frame1.SetTexture("images/Enter"); - frame1.Color = Color.Red; - frame1.Width = 100; - frame1.Height = 100; - frame1.OnClick += delegate(Frame frame, Frame.MouseEventArgs args) - { - if (frame.Color == Color.Red) - frame.Color = Color.Green; - else - frame.Color = Color.Red; - }; - var frame1_child = new GUI.Frame(frame1); - frame1_child.SetTexture("images/Enter"); - frame1_child.Color = Color.Purple; - frame1_child.Width = 60; - frame1_child.Height = 60; - frame1_child.X = 20; - frame1_child.Y = 20; - - var frame2 = new GUI.Frame(); - frame2.Layer = 0; - frame2.SetTexture("images/Enter"); - frame2.Color = Color.Blue; - frame2.Width = 100; - frame2.Height = 100; - frame2.X = 50; - frame2.Y = 50; - frame2.OnClick += delegate(Frame frame, Frame.MouseEventArgs args) - { - if (frame.Color == Color.Blue) - frame.Color = Color.Yellow; - else - frame.Color = Color.Blue; - };*/ - } - - private GUI.Frame CreatePanicFrame() - { - var frame = new GUI.Frame(UIParent); - frame.SetTexture("images/GUI/unit_background"); - frame.Color = Color.Black; - frame.Width = 164; - frame.Height = 19; - - frame.OnClick += delegate(Frame frame1, GUIManager.MouseEventArgs args) - { - DynamicGroupManager.Group group = (DynamicGroupManager.Group)frame1["group"]; - if (group != null) - group.Panic += 5; - }; - - var panicBarBg = new GUI.Frame(frame); - panicBarBg.SetTexture("images/GUI/health_background"); - panicBarBg.Width = 136; - panicBarBg.Height = 13; - panicBarBg.X = 3; - panicBarBg.Y = 3; - frame["panicBarBg"] = panicBarBg; - - var panicBar = new GUI.Frame(panicBarBg); - panicBar.SetTexture("images/GUI/bar_solid"); - panicBar.Color = new Color(87, 55, 253); - panicBar.Width = (int)(panicBarBg.Width * 0.5); - panicBar.Height = 11; - panicBar.X = 1; - panicBar.Y = 1; - frame["panicBar"] = panicBar; - - var text = new GUI.Text(frame); - text.SetFont("fonts/UnitName"); - text.Value = "0%"; - text.X = 4; - text.Y = 1; - frame["text"] = text; - - return frame; - } - - private GUI.Frame CreateUnitFrame(Soldier soldier) - { - var frame = new GUI.Frame(UIParent); - frame.SetTexture("images/GUI/unit_background"); - frame.Color = Color.Black; - frame.Width = 164; - frame.Height = 35; - - var nameText = new GUI.Text(frame); - nameText.SetFont("fonts/UnitName"); - nameText.Value = soldier.Name + " " + soldier.GetHashCode(); - nameText.X = 4; - nameText.Y = 1; - frame["nameText"] = nameText; - - var healthBarBg = new GUI.Frame(frame); - healthBarBg.SetTexture("images/GUI/health_background"); - healthBarBg.Width = 136; - healthBarBg.Height = 13; - healthBarBg.X = 3; - healthBarBg.Y = 19; - frame["healthBarBg"] = healthBarBg; - - var healthBar = new GUI.Frame(healthBarBg); - healthBar.SetTexture("images/GUI/bar_solid"); - healthBar.Color = Color.Red; - healthBar.Width = (int)((healthBarBg.Width - 2) * (soldier.GetComponent().HP / soldier.GetComponent().MaxHP)); - healthBar.Height = 11; - healthBar.X = 1; - healthBar.Y = 1; - frame["healthBar"] = healthBar; - - return frame; - } - - public void CreateUnitFrames(List squad) - { - // TODO: ForEach squad in Squads... - /*GUI.Frame lastFrame = null; - foreach (Soldier soldier in squad) - { - // TODO: Remove this test ;) - soldier.GetComponent().HP = random.Next(0, (int) soldier.GetComponent().MaxHP - 1); - - var uFrame = CreateUnitFrame(soldier); - uFrame["soldier"] = soldier; - soldier["unitFrame"] = uFrame; - if (lastFrame != null) - { - uFrame.X = lastFrame.X; - uFrame.Y = lastFrame.Y + lastFrame.Height; - } - uFrame.OnClick += delegate(Frame f, GUIManager.MouseEventArgs e) - { - var s = (Soldier) f["soldier"]; - KeyboardState ks = Keyboard.GetState(); - - if (!ks.IsKeyDown(Keys.LeftControl)) - { - // Deselect all units - foreach (var unitFrame in UnitFrames) - { - ((Soldier) unitFrame["soldier"]).Selected = false; - unitFrame.Color = Color.Black; - } - } - - if (!s.Selected) - { - s.Selected = true; - uFrame.Color = Color.Blue; - } - }; - UnitFrames.Add(uFrame); - lastFrame = uFrame; - }*/ - - foreach (var soldier in squad) - { - PanicFrames.Add(CreatePanicFrame()); - - // TODO: Remove this test ;) - soldier.GetComponent().HP = random.Next(0, (int)soldier.GetComponent().MaxHP - 1); - - var uFrame = CreateUnitFrame(soldier); - uFrame["soldier"] = soldier; - soldier["unitFrame"] = uFrame; - uFrame.OnClick += delegate(Frame f, GUIManager.MouseEventArgs e) - { - var s = (Soldier)f["soldier"]; - KeyboardState ks = Keyboard.GetState(); - - if (!ks.IsKeyDown(Keys.LeftControl)) - { - // Deselect all units - foreach (var unitFrame in UnitFrames) - { - ((Soldier)unitFrame["soldier"]).Selected = false; - unitFrame.Color = Color.Black; - } - } - - if (!s.Selected) - { - s.Selected = true; - uFrame.Color = Color.Blue; - } - }; - UnitFrames.Add(uFrame); - } - } - - public void UpdateUnitFrames() - { - core.GroupManager.UpdateGroups(); - var groups = core.GroupManager.Groups; - - foreach (var panicFrame in PanicFrames) - panicFrame.Visible = false; - - GUI.Frame lastFrame = null; - for (int index = 0; index < groups.Count; index++) - { - var group = groups[index]; - - var pFrame = PanicFrames[index]; - pFrame["group"] = group; - var pFrameBarBg = (GUI.Frame)pFrame["panicBarBg"]; - var pFrameBar = (GUI.Frame)pFrame["panicBar"]; - var pFrameText = (GUI.Text)pFrame["text"]; - pFrameBar.Width = (int)(pFrameBarBg.Width * (group.Panic / group.MaxPanic)); - pFrameText.Value = group.Panic.ToString() + " (" + (group.Panic / group.MaxPanic * 100f).ToString() + "%)"; - - pFrame.Visible = true; - if (lastFrame != null) - { - pFrame.X = lastFrame.X; - pFrame.Y = lastFrame.Y + lastFrame.Height + 15; - } - lastFrame = pFrame; - - foreach (Soldier soldier in @group.Entities) - { - var frame = (GUI.Frame) soldier["unitFrame"]; - frame.X = lastFrame.X; - frame.Y = lastFrame.Y + lastFrame.Height; - lastFrame = frame; - } - } - } - - public void Update(GameTime gameTime) - { - MouseState ms = Mouse.GetState(); - var mousePos = new Point(ms.X, ms.Y); - var mouseRectangle = new Rectangle(mousePos.X, mousePos.Y, 1, 1); - var mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(mousePos.X, mousePos.Y)); - var mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1); - - // Update the selection rectangle - var selectionFrame = (GUI.Frame)UIParent["selectionFrame"]; - if (selectionFrame.Visible) - { - selectionFrame.Width = (int)mousePos.X - selectionFrame.X; - selectionFrame.Height = (int)mousePos.Y - selectionFrame.Y; - } - - // Update rotations - if (checkingDirection) - { - foreach (var unit in core.Squad) - if (unit.Selected) - { - float directionX = mouseWorldPos.X - unit.Transform.World.X; - float directionY = mouseWorldPos.Y - unit.Transform.World.Y; - //var mouseDirection = mouseWorldPos; - //mouseDirection.Normalize(); - unit.GetComponent().World.Rotation = - (float) - Math.Atan2(mouseWorldPos.Y - unit.Transform.World.Y - unit.Transform.World.Origin.Y, - mouseWorldPos.X - unit.Transform.World.X - unit.Transform.World.Origin.X); - } - } - - UpdateUnitFrames(); - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using DepthsBelow.GUI; +using Microsoft.Xna.Framework; +using System.Diagnostics; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using Microsoft.Xna.Framework.Content; + +namespace DepthsBelow +{ + public class Interface + { + Core core; + + /// + /// The master frame covering the whole game screen. Parent of all frames. + /// This frame handles all mouse interaction with the game world + /// + public Frame UIParent; + + public List PanicFrames = new List(); + public List UnitFrames = new List(); + public Dictionary SoldierToFrame = new Dictionary(); + + Random random = new Random(); + + private bool checkingDirection; + private Vector2 directionStart; + + public Interface(Core core) + { + this.core = core; + + UIParent = new Frame(); + UIParent.Width = GameServices.GetService().Viewport.Width; + UIParent.Height = GameServices.GetService().Viewport.Height; + + // Selection rectangle + var selectionFrame = new Frame(UIParent); + selectionFrame.Texture = Utility.GetSolidTexture(); + selectionFrame.Color = Color.Red * 0.5f; + selectionFrame.Visible = false; + UIParent["selectionFrame"] = selectionFrame; + + // OnClick + UIParent.OnClick += delegate(Frame frame, GUIManager.MouseEventArgs args) + { + KeyboardState ks = Keyboard.GetState(); + MouseState ms = args.MouseState; + + var mousePos = new Point(ms.X, ms.Y); + var mouseRectangle = new Rectangle(mousePos.X, mousePos.Y, 1, 1); + var mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(mousePos.X, mousePos.Y)); + var mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1); + + if (args.LeftButton == ButtonState.Pressed) + { + Debug.WriteLine("Left button pressed on UIParent"); + // Deselect all units + /*if (!ks.IsKeyDown(Keys.LeftControl)) + { + foreach (var unit in core.Squad) + unit.Selected = false; + + foreach (var unitFrame in UnitFrames) + unitFrame.Color = Color.Black; + } + + // Select all units in the rectangle + foreach (var soldier in core.Squad) + { + if (mouseWorldRectangle.Intersects(soldier.GetComponent().Rectangle)) + { + soldier.Selected = true; + + foreach (var unitFrame in UnitFrames) + { + if (unitFrame["soldier"] == soldier) + unitFrame.Color = Color.Blue; + } + } + }*/ + } + + + + // Hide the selection rectangle + //selectionRectangle = Rectangle.Empty; + }; + + // OnPress + UIParent.OnPress += delegate(Frame frame, GUIManager.MouseEventArgs args) + { + //KeyboardState ks = Keyboard.GetState(); + MouseState ms = args.MouseState; + var mousePos = new Point(ms.X, ms.Y); + var mouseRectangle = new Rectangle(mousePos.X, mousePos.Y, 1, 1); + var mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(mousePos.X, mousePos.Y)); + var mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1); + + // Show the selection frame + if (args.LeftButton == ButtonState.Pressed) + { + var selectFrame = (GUI.Frame)frame["selectionFrame"]; + if (!selectFrame.Visible) + { + selectFrame.X = mousePos.X; + selectFrame.Y = mousePos.Y; + selectFrame.Visible = true; + } + } + + if (args.RightButton == ButtonState.Pressed) + { + // Unit rotation + foreach (var unit in core.Squad) + { + if (unit.Selected && mouseWorldRectangle.Intersects(unit.GetComponent().Rectangle)) + { + checkingDirection = true; + directionStart = unit.Transform.World + unit.Transform.World.Origin; + } + } + } + }; + + // OnRelease + UIParent.OnRelease += delegate(Frame frame, GUIManager.MouseEventArgs args) + { + KeyboardState ks = Keyboard.GetState(); + MouseState ms = args.MouseState; + var mousePos = new Point(ms.X, ms.Y); + var mouseRectangle = new Rectangle(mousePos.X, mousePos.Y, 1, 1); + var mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(mousePos.X, mousePos.Y)); + var mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1); + + if (args.LeftButton == ButtonState.Pressed) + { + // Deselect all units + if (!ks.IsKeyDown(Keys.LeftControl)) + { + foreach (var unit in core.Squad) + unit.Selected = false; + + foreach (var unitFrame in UnitFrames) + unitFrame.Color = Color.Black; + } + + // Select all soldier units inside the selection rectangle + var selectFrame = (GUI.Frame)frame["selectionFrame"]; + if (selectFrame.Visible) + { + Console.WriteLine("Before: " + selectFrame.AbsoluteRectangle.X + ", " + selectFrame.AbsoluteRectangle.Y + ", " + selectFrame.AbsoluteRectangle.Width + ", " + selectFrame.AbsoluteRectangle.Height); + + var intersectionRectangle = core.Camera.ScreenToWorld(selectFrame.AbsoluteRectangle); + + Console.WriteLine("After: " + intersectionRectangle.X + ", " + intersectionRectangle.Y + ", " + intersectionRectangle.Width + ", " + intersectionRectangle.Height); + + + // Select all units in the rectangle + foreach (var soldier in core.Squad) + { + if (intersectionRectangle.Intersects(soldier.GetComponent().Rectangle)) + { + soldier.Selected = true; + foreach (var unitFrame in UnitFrames) + { + if (unitFrame["soldier"] == soldier) + unitFrame.Color = Color.Blue; + } + } + } + + selectFrame.Visible = false; + } + } + + // Send orders with right click + if (args.RightButton == ButtonState.Pressed) + { + if (checkingDirection) + { + checkingDirection = false; + } + else + { + Debug.WriteLine("Right button pressed on UIParent"); + foreach (var unit in core.Squad) + if (unit.Selected) + unit.GetComponent().Goal = Grid.WorldToGrid(mouseWorldPos); + } + } + }; + + var button = new GUI.Button(UIParent); + button.SetTexture("images/Enter"); + button.X = 200; + button.Y = 100; + button.Width = 55; + button.Height = 40; + + // Test GUI frames + /*var frame = new GUI.Frame() + { + Y = 100 + }; + + var button = new GUI.Button(frame) + { + Width = 48, + Height = 23, + Texture = core.Content.Load("images/Enter"), + OnClick = delegate(Point clickPos) + { + Debug.WriteLine("That ugly button was clicked at (" + clickPos.X + "," + clickPos.Y + ")"); + } + }; + + var text = new GUI.Text(frame); + text.SetFont("Arial"); + text.Value = "Hello world!";*/ + + // Create some unit frames + /*var panicFrame = CreatePanicFrame(); + var unit1 = CreateUnitFrame("Flight captain Rainbow"); + unit1.X = panicFrame.X; + unit1.Y = panicFrame.Y + panicFrame.Height; + unit1.OnClick += delegate(Frame frame, Frame.MouseEventArgs args) + { + frame.Color = Color.Red; + }; + var unit2 = CreateUnitFrame("Mr. Sparkle"); + unit2.X = unit1.X; + unit2.Y = unit1.Y + unit1.Height; + unit2.OnClick += delegate(Frame frame, Frame.MouseEventArgs args) + { + frame.Color = Color.Red; + };*/ + + /*var frame1 = new GUI.Frame(); + frame1.Layer = 0; + frame1.SetTexture("images/Enter"); + frame1.Color = Color.Red; + frame1.Width = 100; + frame1.Height = 100; + frame1.OnClick += delegate(Frame frame, Frame.MouseEventArgs args) + { + if (frame.Color == Color.Red) + frame.Color = Color.Green; + else + frame.Color = Color.Red; + }; + var frame1_child = new GUI.Frame(frame1); + frame1_child.SetTexture("images/Enter"); + frame1_child.Color = Color.Purple; + frame1_child.Width = 60; + frame1_child.Height = 60; + frame1_child.X = 20; + frame1_child.Y = 20; + + var frame2 = new GUI.Frame(); + frame2.Layer = 0; + frame2.SetTexture("images/Enter"); + frame2.Color = Color.Blue; + frame2.Width = 100; + frame2.Height = 100; + frame2.X = 50; + frame2.Y = 50; + frame2.OnClick += delegate(Frame frame, Frame.MouseEventArgs args) + { + if (frame.Color == Color.Blue) + frame.Color = Color.Yellow; + else + frame.Color = Color.Blue; + };*/ + } + + private GUI.Frame CreatePanicFrame() + { + var frame = new GUI.Frame(UIParent); + frame.SetTexture("images/GUI/unit_background"); + frame.Color = Color.Black; + frame.Width = 164; + frame.Height = 19; + + frame.OnClick += delegate(Frame frame1, GUIManager.MouseEventArgs args) + { + DynamicGroupManager.Group group = (DynamicGroupManager.Group)frame1["group"]; + if (group != null) + group.Panic += 5; + }; + + var panicBarBg = new GUI.Frame(frame); + panicBarBg.SetTexture("images/GUI/health_background"); + panicBarBg.Width = 136; + panicBarBg.Height = 13; + panicBarBg.X = 3; + panicBarBg.Y = 3; + frame["panicBarBg"] = panicBarBg; + + var panicBar = new GUI.Frame(panicBarBg); + panicBar.SetTexture("images/GUI/bar_solid"); + panicBar.Color = new Color(87, 55, 253); + panicBar.Width = (int)(panicBarBg.Width * 0.5); + panicBar.Height = 11; + panicBar.X = 1; + panicBar.Y = 1; + frame["panicBar"] = panicBar; + + var text = new GUI.Text(frame); + text.SetFont("fonts/UnitName"); + text.Value = "0%"; + text.X = 4; + text.Y = 1; + frame["text"] = text; + + return frame; + } + + private GUI.Frame CreateUnitFrame(Soldier soldier) + { + var frame = new GUI.Frame(UIParent); + frame.SetTexture("images/GUI/unit_background"); + frame.Color = Color.Black; + frame.Width = 164; + frame.Height = 35; + + var nameText = new GUI.Text(frame); + nameText.SetFont("fonts/UnitName"); + nameText.Value = soldier.Name + " "/* + soldier.GetHashCode()*/; + nameText.X = 4; + nameText.Y = 1; + frame["nameText"] = nameText; + + var healthBarBg = new GUI.Frame(frame); + healthBarBg.SetTexture("images/GUI/health_background"); + healthBarBg.Width = 136; + healthBarBg.Height = 13; + healthBarBg.X = 3; + healthBarBg.Y = 19; + frame["healthBarBg"] = healthBarBg; + + var healthBar = new GUI.Frame(healthBarBg); + healthBar.SetTexture("images/GUI/bar_solid"); + healthBar.Color = Color.Red; + healthBar.Width = (int)((healthBarBg.Width - 2) * (soldier.GetComponent().HP / soldier.GetComponent().MaxHP)); + healthBar.Height = 11; + healthBar.X = 1; + healthBar.Y = 1; + frame["healthBar"] = healthBar; + + return frame; + } + + public void CreateUnitFrames(List squad) + { + // TODO: ForEach squad in Squads... + /*GUI.Frame lastFrame = null; + foreach (Soldier soldier in squad) + { + // TODO: Remove this test ;) + soldier.GetComponent().HP = random.Next(0, (int) soldier.GetComponent().MaxHP - 1); + + var uFrame = CreateUnitFrame(soldier); + uFrame["soldier"] = soldier; + soldier["unitFrame"] = uFrame; + if (lastFrame != null) + { + uFrame.X = lastFrame.X; + uFrame.Y = lastFrame.Y + lastFrame.Height; + } + uFrame.OnClick += delegate(Frame f, GUIManager.MouseEventArgs e) + { + var s = (Soldier) f["soldier"]; + KeyboardState ks = Keyboard.GetState(); + + if (!ks.IsKeyDown(Keys.LeftControl)) + { + // Deselect all units + foreach (var unitFrame in UnitFrames) + { + ((Soldier) unitFrame["soldier"]).Selected = false; + unitFrame.Color = Color.Black; + } + } + + if (!s.Selected) + { + s.Selected = true; + uFrame.Color = Color.Blue; + } + }; + UnitFrames.Add(uFrame); + lastFrame = uFrame; + }*/ + + foreach (var soldier in squad) + { + PanicFrames.Add(CreatePanicFrame()); + + // TODO: Remove this test ;) + soldier.GetComponent().HP = random.Next(0, (int)soldier.GetComponent().MaxHP - 1); + + var uFrame = CreateUnitFrame(soldier); + uFrame["soldier"] = soldier; + soldier["unitFrame"] = uFrame; + uFrame.OnClick += delegate(Frame f, GUIManager.MouseEventArgs e) + { + var s = (Soldier)f["soldier"]; + KeyboardState ks = Keyboard.GetState(); + + if (!ks.IsKeyDown(Keys.LeftControl)) + { + // Deselect all units + foreach (var unitFrame in UnitFrames) + { + ((Soldier)unitFrame["soldier"]).Selected = false; + unitFrame.Color = Color.Black; + } + } + + if (!s.Selected) + { + s.Selected = true; + uFrame.Color = Color.Blue; + } + }; + UnitFrames.Add(uFrame); + } + } + + public void UpdateUnitFrames() + { + core.GroupManager.UpdateGroups(); + var groups = core.GroupManager.Groups; + + foreach (var panicFrame in PanicFrames) + panicFrame.Visible = false; + + GUI.Frame lastFrame = null; + for (int index = 0; index < groups.Count; index++) + { + var group = groups[index]; + + var pFrame = PanicFrames[index]; + pFrame["group"] = group; + var pFrameBarBg = (GUI.Frame)pFrame["panicBarBg"]; + var pFrameBar = (GUI.Frame)pFrame["panicBar"]; + var pFrameText = (GUI.Text)pFrame["text"]; + pFrameBar.Width = (int)(pFrameBarBg.Width * (group.Panic / group.MaxPanic)); + pFrameText.Value = group.Panic.ToString() + " (" + (group.Panic / group.MaxPanic * 100f).ToString() + "%)"; + + pFrame.Visible = true; + if (lastFrame != null) + { + pFrame.X = lastFrame.X; + pFrame.Y = lastFrame.Y + lastFrame.Height + 15; + } + lastFrame = pFrame; + + foreach (Soldier soldier in @group.Entities) + { + var frame = (GUI.Frame) soldier["unitFrame"]; + frame.X = lastFrame.X; + frame.Y = lastFrame.Y + lastFrame.Height; + lastFrame = frame; + } + } + } + + public void Update(GameTime gameTime) + { + MouseState ms = Mouse.GetState(); + var mousePos = new Point(ms.X, ms.Y); + var mouseRectangle = new Rectangle(mousePos.X, mousePos.Y, 1, 1); + var mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(mousePos.X, mousePos.Y)); + var mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1); + + // Update the selection rectangle + var selectionFrame = (GUI.Frame)UIParent["selectionFrame"]; + if (selectionFrame.Visible) + { + selectionFrame.Width = (int)mousePos.X - selectionFrame.X; + selectionFrame.Height = (int)mousePos.Y - selectionFrame.Y; + } + + // Update rotations + if (checkingDirection) + { + foreach (var unit in core.Squad) + if (unit.Selected) + { + float directionX = mouseWorldPos.X - unit.Transform.World.X; + float directionY = mouseWorldPos.Y - unit.Transform.World.Y; + //var mouseDirection = mouseWorldPos; + //mouseDirection.Normalize(); + unit.GetComponent().World.Rotation = + (float) + Math.Atan2(mouseWorldPos.Y - unit.Transform.World.Y - unit.Transform.World.Origin.Y, + mouseWorldPos.X - unit.Transform.World.X - unit.Transform.World.Origin.X); + } + } + + UpdateUnitFrames(); + } + } +} diff --git a/DepthsBelow/DepthsBelow/KeyboardInput.cs b/DepthsBelow/DepthsBelow/KeyboardInput.cs index 78e33f8..8c21677 100755 --- a/DepthsBelow/DepthsBelow/KeyboardInput.cs +++ b/DepthsBelow/DepthsBelow/KeyboardInput.cs @@ -105,10 +105,10 @@ namespace DepthsBelow { 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)) + // 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 @@ -177,6 +177,10 @@ namespace DepthsBelow }*/ core.TestMonster.X = 12; core.TestMonster.Y = 4; + } + if (ks.IsKeyDown(Keys.N)) + { + } if (ks.IsKeyDown(Keys.S)) { diff --git a/DepthsBelow/DepthsBelow/SmallEnemy.cs b/DepthsBelow/DepthsBelow/SmallEnemy.cs index 49763f5..4241c3e 100755 --- a/DepthsBelow/DepthsBelow/SmallEnemy.cs +++ b/DepthsBelow/DepthsBelow/SmallEnemy.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; using DepthsBelow.Component; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; @@ -31,7 +31,7 @@ namespace DepthsBelow } } - public SmallEnemy(EntityManager entityManager, ref List swarm) + public SmallEnemy(EntityManager entityManager, ref List swarm) : base(entityManager) { if (Texture == null) @@ -49,16 +49,16 @@ namespace DepthsBelow AddComponent(cc); var pfc = new PathFinder(this); - AddComponent(pfc); - - var stat = new Component.Stat(this) - { - Life = 2, - Defence = 0, - Strength = 5000, - GetAim = 100, - GetDodge = 50 - }; + AddComponent(pfc); + + var stat = new Component.Stat(this) + { + Life = 2, + Defence = 0, + Strength = 5000, + GetAim = 100, + GetDodge = 50 + }; AddComponent(stat); } @@ -73,7 +73,7 @@ namespace DepthsBelow } public static void LoadContent() - { + { Texture = GameServices.GetService().Load("images/Monster"); } @@ -111,14 +111,25 @@ namespace DepthsBelow // HACK: Make this work properly with other speeds... float speed = 4f; if (Transform.World.X < nodeWorldPos.X) + { Transform.World.X += speed; + GetComponent().World.Rotation = MathHelper.ToRadians(0); + } if (Transform.World.X > nodeWorldPos.X) + { Transform.World.X -= speed; + GetComponent().World.Rotation = MathHelper.ToRadians(180); + } if (Transform.World.Y < nodeWorldPos.Y) + { Transform.World.Y += speed; + GetComponent().World.Rotation = MathHelper.ToRadians(90); + } if (Transform.World.Y > nodeWorldPos.Y) + { Transform.World.Y -= speed; - + GetComponent().World.Rotation = MathHelper.ToRadians(270); + } if (Transform.World == nodeWorldPos) { if (currentStep < numberOfSteps)