root/src/AIDemos/Screens/Demos/Demo4.cs
| 4 | 15 | ||
|---|---|---|---|
15 | using Brains.Framework.Behaviors; | 15 | using Brains.Framework.Behaviors; |
16 | using AIDemos.GameClasses.Behaviors; | 16 | using AIDemos.GameClasses.Behaviors; |
17 | using System.Collections; | 17 | using System.Collections; |
18 | using Brains.Framework.Locomotion; | ||
18 | 19 | ||
19 | namespace AIDemos.Screens.Demos | 20 | namespace AIDemos.Screens.Demos |
20 | { | 21 | { |
21 | public class Demo4 : DemoGameScreen | 22 | public class Demo4 : DemoGameScreen |
22 | { | 23 | { |
23 | AIEngine _engine; | 24 | AIEngine _engine; |
24 | 25 | | |
25 | public Demo4() | 26 | public Demo4() |
26 | { | 27 | { |
27 | TransitionOnTime = TimeSpan.FromSeconds(1.5); | 28 | TransitionOnTime = TimeSpan.FromSeconds(1.5); |
28 | TransitionOffTime = TimeSpan.FromSeconds(0.5); | 29 | TransitionOffTime = TimeSpan.FromSeconds(0.5); |
29 | Title = "Demo 4 - A Bigger World"; | 30 | Title = "Demo 4 - Steering Motion Controller"; |
30 | Description = "An Agent is added to the world and set to patrol an area."; | 31 | Description = ""; |
31 | | ||
32 | | ||
33 | } | 32 | } |
34 | 33 | ||
35 | public override void LoadContent() | 34 | public override void LoadContent() |
... | ... | ||
40 | //primitiveBatch.CameraPosition = new Vector2(-(_engine.World.Map.Cluster[0].Width / 2), -(_engine.World.Map.Cluster[0].Height / 2)); | 39 | //primitiveBatch.CameraPosition = new Vector2(-(_engine.World.Map.Cluster[0].Width / 2), -(_engine.World.Map.Cluster[0].Height / 2)); |
41 | ((DrawableWorld)_engine.World).Font = gameFont; | 40 | ((DrawableWorld)_engine.World).Font = gameFont; |
42 | ((DrawableWorld)_engine.World).Batch = ScreenManager.SpriteBatch; | 41 | ((DrawableWorld)_engine.World).Batch = ScreenManager.SpriteBatch; |
43 | | ||
44 | 42 | ||
45 | 43 | primitiveBatch.CameraPosition = new Vector2( | |
44 | -ScreenManager.GraphicsDevice.Viewport.Width / 2 + | ||
45 | _engine.World.Map.ClusterGrid.Grids[0].Width / 2, | ||
46 | -ScreenManager.GraphicsDevice.Viewport.Height / 2 + | ||
47 | _engine.World.Map.ClusterGrid.Grids[0].Height / 2); | ||
46 | ScreenManager.Game.ResetElapsedTime(); | 48 | ScreenManager.Game.ResetElapsedTime(); |
47 | } | 49 | } |
48 | private void LoadAI() | 50 | private void LoadAI() |
... | ... | ||
50 | _engine = new AIEngine(); | 52 | _engine = new AIEngine(); |
51 | _engine.CreateWorld(new DrawableWorld()); | 53 | _engine.CreateWorld(new DrawableWorld()); |
52 | _engine.World.LoadMapDataFromTexture( | 54 | _engine.World.LoadMapDataFromTexture( |
53 | content.Load<Texture2D>("DemoTextures/MAPDemo3"), | 55 | content.Load<Texture2D>("DemoTextures/MAPDemoBlank8"), |
54 | 8, | 56 | 64, |
55 | 8, | ||
56 | 32, | ||
57 | typeof(DrawableGrid)); | 57 | typeof(DrawableGrid)); |
58 | 58 | ||
59 | GridCell fromcell=_engine.World.Map.AllCells.First<GridCell>( | ||
60 | a => a.Labels[AIConsts.COLORR]==0&& | ||
61 | a.Labels[AIConsts.COLORG]==255 && | ||
62 | a.Labels[AIConsts.COLORB]==0); | ||
63 | GridCell tocell = _engine.World.Map.AllCells.First<GridCell>( | ||
64 | a => a.Labels[AIConsts.COLORR] == 255 && | ||
65 | a.Labels[AIConsts.COLORG] == 0 && | ||
66 | a.Labels[AIConsts.COLORB] == 0); | ||
67 | GameActor actor = new GameActor(fromcell.Position, 5); | ||
68 | actor.Locomotion = new Brains.Framework.Locomotion.LocomtionSteering(); | ||
69 | actor.Locomotion.MaxSpeed = 50; | ||
70 | actor.Locomotion.MaxRotation = MathHelper.TwoPi; | ||
71 | 59 | ||
72 | _engine.World.AddActor(actor); | ||
73 | SequenceBehavior _sequence = new SequenceBehavior(); | ||
74 | 60 | ||
75 | 61 | _actor = new GameActor(new Vector2(32, 32), 5); | |
76 | DrawableBehaviorGoTo _goto = new DrawableBehaviorGoTo(); | 62 | _actor.Locomotion = new LocomtionSteering(); |
77 | _goto.StartNode = fromcell; | 63 | _actor.Locomotion.MaxSpeed = 50; |
78 | _goto.EndNode = tocell; | 64 | _actor.Locomotion.MaxRotation = MathHelper.TwoPi; |
79 | _sequence.SubBehaviors.Add(_goto); | 65 | _engine.World.AddActor(_actor); |
80 | _sequence.SubBehaviors.Add(new DieBehavior()); | ||
81 | actor.RootBehavior = _sequence; | ||
82 | 66 | ||
67 | SwitchMode(DemoMode.Seek); | ||
83 | } | 68 | } |
84 | class CellComparer:IComparer | ||
85 | { | ||
86 | | ||
87 | public int Compare(object x, object y) | ||
88 | { | ||
89 | GridCell p1; | ||
90 | GridCell p2; | ||
91 | 69 | ||
92 | if (x is GridCell) | ||
93 | p1 = x as GridCell; | ||
94 | else | ||
95 | throw new ArgumentException("Object is not of type GridCell."); | ||
96 | |||
97 | if (y is GridCell) | ||
98 | p2 = y as GridCell; | ||
99 | else | ||
100 | throw new ArgumentException("Object is not of type GridCell."); | ||
101 | |||
102 | return p1.Labels[AIConsts.COLORR].CompareTo(p2.Labels[AIConsts.COLORR]); | ||
103 | } | ||
104 | } | ||
105 | 70 | ||
106 | GridCell _endNode; | 71 | GameActor _actor; |
72 | | ||
107 | public override void UnloadContent() | 73 | public override void UnloadContent() |
108 | { | 74 | { |
109 | content.Unload(); | 75 | content.Unload(); |
... | ... | ||
144 | } | 110 | } |
145 | else | 111 | else |
146 | { | 112 | { |
147 | 113 | Vector2 mousPos = new Vector2(_currentMouse.X, _currentMouse.Y); | |
114 | Vector2 mapMousePos = mousPos + primitiveBatch.CameraPosition; | ||
115 | | ||
116 | if (IsNewLeftMouseClick()) | ||
117 | { | ||
118 | SetSeekPosition(mapMousePos); | ||
119 | } | ||
120 | |||
121 | if (IsNewKeyPress(input, Keys.F1)) | ||
122 | SwitchMode(DemoMode.Seek); | ||
123 | | ||
124 | if (IsNewKeyPress(input,Keys.F2)) | ||
125 | SwitchMode(DemoMode.Arrive); | ||
126 | if (IsNewKeyPress(input, Keys.F3)) | ||
127 | SwitchMode(DemoMode.Wander); | ||
128 | | ||
148 | } | 129 | } |
149 | 130 | ||
150 | base.HandleInput(input); | 131 | base.HandleInput(input); |
151 | } | 132 | } |
152 | 133 | ||
134 | private void SetSeekPosition(Vector2 mapMousePos) | ||
135 | { | ||
136 | ((LocomtionSteering)_actor.Locomotion).SeekPos = mapMousePos; | ||
137 | } | ||
138 | |||
139 | DemoMode _currentMode; | ||
140 | private void SwitchMode(DemoMode demoMode) | ||
141 | { | ||
142 | _currentMode = demoMode; | ||
143 | ((LocomtionSteering)_actor.Locomotion).TurnAllOff(); | ||
144 | switch (demoMode) | ||
145 | { | ||
146 | case DemoMode.Seek: | ||
147 | ((LocomtionSteering)_actor.Locomotion).TurnOnSeek(new Vector2(400)); | ||
148 | break; | ||
149 | case DemoMode.Arrive: | ||
150 | ((LocomtionSteering)_actor.Locomotion).TurnOnArrive(new Vector2(400)); | ||
151 | break; | ||
152 | case DemoMode.Wander: | ||
153 | ((LocomtionSteering)_actor.Locomotion).TurnOnWander(); | ||
154 | break; | ||
155 | } | ||
156 | } | ||
157 | |||
158 | enum DemoMode | ||
159 | { | ||
160 | Seek, | ||
161 | Arrive, | ||
162 | Wander | ||
163 | } | ||
164 | private bool IsNewKeyPress(InputState input, Keys keys) | ||
165 | { | ||
166 | return input.CurrentKeyboardStates[0].IsKeyUp(keys) && | ||
167 | input.LastKeyboardStates[0].IsKeyDown(keys); | ||
168 | } | ||
169 | |||
170 | private bool IsNewLeftMouseClick() | ||
171 | { | ||
172 | return _currentMouse.LeftButton == ButtonState.Released && | ||
173 | _lastMouse.LeftButton == ButtonState.Pressed; | ||
174 | } | ||
175 | |||
153 | public override void Draw(GameTime gameTime) | 176 | public override void Draw(GameTime gameTime) |
154 | { | 177 | { |
155 | ScreenManager.GraphicsDevice.Clear(ClearOptions.Target, | 178 | ScreenManager.GraphicsDevice.Clear(ClearOptions.Target, |
... | ... | ||
158 | SpriteBatch spriteBatch = ScreenManager.SpriteBatch; | 181 | SpriteBatch spriteBatch = ScreenManager.SpriteBatch; |
159 | 182 | ||
160 | spriteBatch.Begin(); | 183 | spriteBatch.Begin(); |
161 | 184 | spriteBatch.DrawString( | |
185 | gameFont, | ||
186 | "Mode: " + _currentMode.ToString(), | ||
187 | new Vector2(ScreenManager.GraphicsDevice.Viewport.Width - 200, 10), | ||
188 | Color.White, | ||
189 | 0, | ||
190 | new Vector2(0), | ||
191 | 0.5f,SpriteEffects.None, | ||
192 | 0); | ||
193 | |||
162 | base.Draw(gameTime); | 194 | base.Draw(gameTime); |
163 | ((IRender)_engine.World).Render(primitiveBatch); | 195 | ((IRender)_engine.World).Render(primitiveBatch); |
164 | spriteBatch.End(); | 196 | spriteBatch.End(); |
165 | 197 | ||
198 | primitiveBatch.DrawCircle(((LocomtionSteering)_actor.Locomotion).SeekPos, 5, Color.Red); | ||
166 | 199 | ||
167 | if (TransitionPosition > 0) | 200 | if (TransitionPosition > 0) |
168 | ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha); | 201 | ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha); |
Download diff