Author: conkerjo
(2009/07/01 23:06) Over 2 years ago
Refactoring
89
<Compile Include="GameClasses\Behaviors\DieBehavior.cs" />
90
<Compile Include="GameClasses\GameActor.cs" />
91
<Compile Include="Properties\AssemblyInfo.cs" />
92
<Compile Include="Screens\Demos\Demo5.cs" />
<Compile Include="Screens\Demos\Demo4.cs" />
93
<Compile Include="Screens\Demos\Demo3.cs" />
94
<Compile Include="Screens\Demos\Demo2.cs" />
95
<Processor>TextureProcessor</Processor>
</Compile>
96
</ItemGroup>
97
<ItemGroup>
98
<Compile Include="DemoTextures\MAPDemoBlank8.png">
99
<Name>MAPDemoBlank8</Name>
100
<Importer>TextureImporter</Importer>
101
102
103
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" />
104
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
105
Other similar extension points exist, see Microsoft.Common.targets.
106
17
public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
18
{
19
base.Update(gameTime);
20
WrapAround();
}
21
22
23
private void WrapAround()
24
25
Vector2 newPosition=Position;
26
Vector2 topLeft = ParentWorld.Map.TopLeft;
27
Vector2 bottomRight=ParentWorld.Map.BottomRight;
28
29
if (Position.X < 0)
30
newPosition.X = bottomRight.X;
31
if (Position.X > bottomRight.X)
32
newPosition.X = topLeft.X;
33
34
if (Position.Y < 0)
35
newPosition.Y = bottomRight.Y;
36
if (Position.Y > bottomRight.Y)
37
newPosition.Y = topLeft.Y;
38
Position = newPosition;
39
40
41
14
using Brains;
15
using Brains.Framework.Behaviors;
16
using AIDemos.GameClasses.Behaviors;
using Brains.Framework.Map;
using Brains.Framework.Utility;
namespace AIDemos.Screens.Demos
...
46
48
47
primitiveBatch.CameraPosition = new Vector2(
49
-ScreenManager.GraphicsDevice.Viewport.Width / 2 +
50
_engine.World.Map.Cluster[0].Width / 2,
51
_engine.World.Map.ClusterGrid.Grids[0].Width / 2,
-ScreenManager.GraphicsDevice.Viewport.Height / 2 +
52
_engine.World.Map.Cluster[0].Height / 2);
53
_engine.World.Map.ClusterGrid.Grids[0].Height / 2);
54
55
ScreenManager.Game.ResetElapsedTime();
56
118
_lastMouse.LeftButton == ButtonState.Pressed)
120
119
121
Vector2 mapMousePos = mousPos + primitiveBatch.CameraPosition;
122
GridCell cell = _engine.World.Map.Cluster[0].GetCellAtPosition(mapMousePos);
123
GridCell cell = _engine.World.Map.ClusterGrid.Grids[0].GetCellAtPosition(mapMousePos);
if (cell != null)
124
125
GameActor actor = new GameActor(cell.Position, 5);
126
using System.Collections;
43
42
44
45
154
156
155
157
158
159
160
161
162
using Brains.Framework.Locomotion;
public class Demo4 : DemoGameScreen
AIEngine _engine;
public Demo4()
TransitionOnTime = TimeSpan.FromSeconds(1.5);
TransitionOffTime = TimeSpan.FromSeconds(0.5);
Title = "Demo 4 - A Bigger World";
Title = "Demo 4 - Steering Motion Controller";
Description = "An Agent is added to the world and set to patrol an area.";
Description = "";
public override void LoadContent()
//primitiveBatch.CameraPosition = new Vector2(-(_engine.World.Map.Cluster[0].Width / 2), -(_engine.World.Map.Cluster[0].Height / 2));
((DrawableWorld)_engine.World).Font = gameFont;
((DrawableWorld)_engine.World).Batch = ScreenManager.SpriteBatch;
private void LoadAI()
_engine = new AIEngine();
_engine.CreateWorld(new DrawableWorld());
_engine.World.LoadMapDataFromTexture(
content.Load<Texture2D>("DemoTextures/MAPDemo3"),
content.Load<Texture2D>("DemoTextures/MAPDemoBlank8"),
8,
64,
32,
57
typeof(DrawableGrid));
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
72
_engine.World.AddActor(actor);
73
SequenceBehavior _sequence = new SequenceBehavior();
74
75
_actor = new GameActor(new Vector2(32, 32), 5);
76
DrawableBehaviorGoTo _goto = new DrawableBehaviorGoTo();
_actor.Locomotion = new LocomtionSteering();
77
_goto.StartNode = fromcell;
_actor.Locomotion.MaxSpeed = 50;
78
_goto.EndNode = tocell;
_actor.Locomotion.MaxRotation = MathHelper.TwoPi;
79
_sequence.SubBehaviors.Add(_goto);
_engine.World.AddActor(_actor);
80
_sequence.SubBehaviors.Add(new DieBehavior());
81
actor.RootBehavior = _sequence;
82
SwitchMode(DemoMode.Seek);
83
84
class CellComparer:IComparer
85
86
87
public int Compare(object x, object y)
88
GridCell p1;
GridCell p2;
if (x is GridCell)
p1 = x as GridCell;
else
throw new ArgumentException("Object is not of type GridCell.");
if (y is GridCell)
p2 = y as GridCell;
return p1.Labels[AIConsts.COLORR].CompareTo(p2.Labels[AIConsts.COLORR]);
GridCell _endNode;
GameActor _actor;
107
public override void UnloadContent()
108
109
content.Unload();
144
110
145
111
146
112
147
113
Vector2 mousPos = new Vector2(_currentMouse.X, _currentMouse.Y);
114
115
116
if (IsNewLeftMouseClick())
117
SetSeekPosition(mapMousePos);
if (IsNewKeyPress(input, Keys.F1))
if (IsNewKeyPress(input,Keys.F2))
SwitchMode(DemoMode.Arrive);
if (IsNewKeyPress(input, Keys.F3))
127
SwitchMode(DemoMode.Wander);
128
148
129
149
130
150
base.HandleInput(input);
131
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();
switch (demoMode)
case DemoMode.Seek:
((LocomtionSteering)_actor.Locomotion).TurnOnSeek(new Vector2(400));
break;
case DemoMode.Arrive:
((LocomtionSteering)_actor.Locomotion).TurnOnArrive(new Vector2(400));
case DemoMode.Wander:
153
((LocomtionSteering)_actor.Locomotion).TurnOnWander();
enum DemoMode
Seek,
Arrive,
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
public override void Draw(GameTime gameTime)
176
177
ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
178
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
181
182
spriteBatch.Begin();
183
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
base.Draw(gameTime);
194
((IRender)_engine.World).Render(primitiveBatch);
195
spriteBatch.End();
196
197
198
primitiveBatch.DrawCircle(((LocomtionSteering)_actor.Locomotion).SeekPos, 5, Color.Red);
199
if (TransitionPosition > 0)
200
ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
201
/// </summary>
class MainMenuScreen : MenuScreen
#region Initialization
/// <summary>
playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
MenuEntries.Add(playGameMenuEntry);
playGameMenuEntry = new MenuEntry("Demo4 - A Bigger World");
playGameMenuEntry = new MenuEntry("Demo4 - Steering Motion Controller");
#endregion
#region Handle Input
/// Event handler for when the Play Game menu entry is selected.
LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
new Demo3());
case "Demo4 - A Bigger World":
case "Demo4 - Steering Motion Controller":
new Demo4());
case "Demo5 - A Bigger World":
new Demo5());
{{{{{_engine.World.Map.ClusterGrid.Grids[0].Width / 2,_engine.World.Map.ClusterGrid.Grids[0].Height / 2);{{GridCell cell = _engine.World.Map.ClusterGrid.Grids[0].GetCellAtPosition(mapMousePos);{{{{_engine.World.Map.ClusterGrid.Grids[0].Width / 2,_engine.World.Map.ClusterGrid.Grids[0].Height / 2);{{GridCell cell = _engine.World.Map.ClusterGrid.Grids[0].GetCellAtPosition(mapMousePos);{{{{{{{{{{Title = "Demo 4 - A Bigger World";Title = "Demo 4 - Steering Motion Controller";Description = "An Agent is added to the world and set to patrol an area.";Description = "";primitiveBatch.CameraPosition = new Vector2(-ScreenManager.GraphicsDevice.Viewport.Width / 2 +_engine.World.Map.ClusterGrid.Grids[0].Width / 2,-ScreenManager.GraphicsDevice.Viewport.Height / 2 +_engine.World.Map.ClusterGrid.Grids[0].Height / 2);content.Load<Texture2D>("DemoTextures/MAPDemo3"),content.Load<Texture2D>("DemoTextures/MAPDemoBlank8"),8,64,8,32,GridCell fromcell=_engine.World.Map.AllCells.First<GridCell>(a => a.Labels[AIConsts.COLORR]==0&&a.Labels[AIConsts.COLORG]==255 &&a.Labels[AIConsts.COLORB]==0);GridCell tocell = _engine.World.Map.AllCells.First<GridCell>(a => a.Labels[AIConsts.COLORR] == 255 &&a.Labels[AIConsts.COLORG] == 0 &&a.Labels[AIConsts.COLORB] == 0);GameActor actor = new GameActor(fromcell.Position, 5);actor.Locomotion = new Brains.Framework.Locomotion.LocomtionSteering();actor.Locomotion.MaxSpeed = 50;actor.Locomotion.MaxRotation = MathHelper.TwoPi;_engine.World.AddActor(actor);SequenceBehavior _sequence = new SequenceBehavior();_actor = new GameActor(new Vector2(32, 32), 5);DrawableBehaviorGoTo _goto = new DrawableBehaviorGoTo();_actor.Locomotion = new LocomtionSteering();_goto.StartNode = fromcell;_actor.Locomotion.MaxSpeed = 50;_goto.EndNode = tocell;_actor.Locomotion.MaxRotation = MathHelper.TwoPi;_sequence.SubBehaviors.Add(_goto);_engine.World.AddActor(_actor);_sequence.SubBehaviors.Add(new DieBehavior());actor.RootBehavior = _sequence;class CellComparer:IComparer{public int Compare(object x, object y){GridCell p1;GridCell p2;if (x is GridCell)p1 = x as GridCell;elsethrow new ArgumentException("Object is not of type GridCell.");if (y is GridCell)p2 = y as GridCell;elsethrow new ArgumentException("Object is not of type GridCell.");return p1.Labels[AIConsts.COLORR].CompareTo(p2.Labels[AIConsts.COLORR]);}}GridCell _endNode;GameActor _actor;{{{{Vector2 mousPos = new Vector2(_currentMouse.X, _currentMouse.Y);Vector2 mapMousePos = mousPos + primitiveBatch.CameraPosition;if (IsNewLeftMouseClick()){SetSeekPosition(mapMousePos);}if (IsNewKeyPress(input, Keys.F1))SwitchMode(DemoMode.Seek);if (IsNewKeyPress(input,Keys.F2))SwitchMode(DemoMode.Arrive);if (IsNewKeyPress(input, Keys.F3))SwitchMode(DemoMode.Wander);{{{{{{{{spriteBatch.DrawString(gameFont,"Mode: " + _currentMode.ToString(),new Vector2(ScreenManager.GraphicsDevice.Viewport.Width - 200, 10),Color.White,0,new Vector2(0),0.5f,SpriteEffects.None,0);{{#region InitializationplayGameMenuEntry = new MenuEntry("Demo4 - A Bigger World");playGameMenuEntry = new MenuEntry("Demo4 - Steering Motion Controller");#endregion#region Handle Inputcase "Demo4 - A Bigger World":case "Demo4 - Steering Motion Controller":#endregion