1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using AIDemos;
using Microsoft.Xna.Framework.Content;
using System.Threading;
using Microsoft.Xna.Framework.Input;
using Brains.Framework;
using AIRendering;
using AIDemos.GameClasses;
using Brains;
using Brains.Framework.Behaviors;
using AIDemos.GameClasses.Behaviors;
using System.Collections;
using Brains.Framework.Map;
using Brains.Framework.Utility;
using AIDemos.GameClasses.Behaviors.Conditions;
using AIDemos.GameClasses.Behaviors.Decorators;
using Brains.Framework.Locomotion;
using Brains.Framework.Grouping;
namespace AIDemos.Screens.Demos
{
public class Demo7 : DemoGameScreen
{
public Demo7():base("Demo 7 - Group Behaviors","")
{
TransitionOnTime = TimeSpan.FromSeconds(1.5);
TransitionOffTime = TimeSpan.FromSeconds(0.5);
}
public override void LoadContent()
{
base.LoadContent();
LoadAI();
CenterCamOnMap();
ScreenManager.Game.ResetElapsedTime();
}
private void LoadAI()
{
_engine.CreateWorld(new DrawableWorld());
ChangeDemoState(DemoState.Seperation);
}
private void ChangeDemoState(DemoState demoState)
{
_demoState = demoState;
_engine = new AIEngine();
_engine.CreateWorld(new DrawableWorld());
Group _group = new Group();
switch (_demoState)
{
case DemoState.Seperation:
_engine.World.SetupMap(400, 400, 64);
float _tmpY = 32;
for (int i = 0; i < 10; i++)
{
Agent boid = AddBoid(new Vector2(32,_tmpY));
_tmpY += 16;
((LocomtionSteering)boid.Locomotion).TurnOnSeek(_engine.World.Map.ClusterGrid.Grids[0].GetCell(4, 4).Position);
((LocomtionSteering)boid.Locomotion).TurnOnCohesion();
((LocomtionSteering)boid.Locomotion).TurnOnAlignment();
((LocomtionSteering)boid.Locomotion).TurnOnSeperation();
_engine.World.AddActor(boid);
_group.AddAgent(boid);
}
break;
}
_engine.World.AddGroup(_group);
CenterCamOnMap();
}
private Agent AddBoid(Vector2 pos)
{
GameActor _actor = new GameActor(pos, 10);
_actor.Locomotion = new Brains.Framework.Locomotion.LocomtionSteering();
_actor.Locomotion.MaxRotation = MathHelper.TwoPi;
_actor.Locomotion.MaxSpeed = 100;
return _actor;
}
DemoState _demoState = DemoState.Seperation;
enum DemoState
{
Seperation,
Alignment,
Cohesion
}
class CellComparer:IComparer
{
public int Compare(object x, object y)
{
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;
else
throw new ArgumentException("Object is not of type GridCell.");
return p1.Labels[AIConsts.COLORR].CompareTo(p2.Labels[AIConsts.COLORR]);
}
}
public override void UnloadContent()
{
content.Unload();
}
public override void Update(GameTime gameTime, bool otherScreenHasFocus,
bool coveredByOtherScreen)
{
base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
if (IsActive)
{
_engine.Update(gameTime);
Vector2 mousPos = new Vector2(_currentMouse.X, _currentMouse.Y);
if (_currentMouse.LeftButton == ButtonState.Released &&
_lastMouse.LeftButton == ButtonState.Pressed)
{
Vector2 mapMousePos = mousPos + primitiveBatch.CameraPosition;
}
}
}
MouseState _currentMouse;
MouseState _lastMouse;
public override void HandleInput(InputState input)
{
if (input == null)
throw new ArgumentNullException("input");
_lastMouse = _currentMouse;
_currentMouse = Mouse.GetState();
int playerIndex = (int)ControllingPlayer.Value;
KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];
bool gamePadDisconnected = !gamePadState.IsConnected &&
input.GamePadWasConnected[playerIndex];
base.HandleInput(input);
}
public override void Draw(GameTime gameTime)
{
ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
Color.Black, 0, 0);
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
spriteBatch.Begin();
base.Draw(gameTime);
((IRender)_engine.World).Render(primitiveBatch);
spriteBatch.End();
if (TransitionPosition > 0)
ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
}
}
} |