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 |
using System;
using System.Collections.Generic;
using System.Linq;
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using ZuneDemo.Renderer;
using Brains.Framework.Map;
using Brains.Framework.Utility;
using Brains.Framework.Locomotion;
using Brains.Framework.Behaviors;
using ZuneDemo.Behaviors;
namespace ZuneDemo
{
public class Game1 : Microsoft.Xna.Framework.Game
{
RenderTarget2D zuneTarget;
private GridCell endNode;
private GraphicsDeviceManager graphics;
private SpriteBatch spriteBatch;
private Brains.Framework.AIEngine engine;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Frame rate is 30 fps by default for Zune.
TargetElapsedTime = TimeSpan.FromSeconds(1 / 30.0);
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
zuneTarget = new RenderTarget2D(GraphicsDevice, 480, 272, 1, SurfaceFormat.Color);
LoadAI();
//CenterCamOnMap();
}
private void LoadAI()
{
this.engine = new Brains.Framework.AIEngine();
this.engine.CreateWorld(new DrawableWorld(this.Content));
this.engine.World.LoadMapDataFromTexture(Content.Load<Texture2D>("Textures/MAPDemo1"),
16,
typeof(DrawableGrid));
//Find the marked green node
endNode = this.engine.World.Map.AllCells.First(
a => a.Labels[AIConsts.COLORG] == 255 &&
a.Labels[AIConsts.COLORR] == 0 &&
a.Labels[AIConsts.COLORB] == 0
);
}
protected override void UnloadContent()
{
}
float timer = 0;
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
this.engine.Update(gameTime);
timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
if (timer > 3)
{
timer = 0;
GridCell cell = this.engine.World.Map.ClusterGrid.Grids[0].GetCellAtPosition(new Vector2(20, 20));
if (cell != null)
{
DrawableAgent actor = new DrawableAgent(this.Content, cell.Position, 5);
actor.Locomotion = new LocomotionController();
actor.Locomotion.MaxSpeed = 50;
actor.Locomotion.MaxRotation = MathHelper.TwoPi;
this.engine.World.AddActor(actor);
SequenceBehavior _sequence = new SequenceBehavior();
GoToBehavior _goto = new GoToBehavior();
_goto.StartNode = cell;
_goto.EndNode = endNode;
_sequence.SubBehaviors.Add(_goto);
_sequence.SubBehaviors.Add(new DieBehavior());
actor.RootBehavior = _sequence;
}
}
base.Update(gameTime);
}
protected override bool BeginDraw()
{
bool val = base.BeginDraw();
if (val)
{
GraphicsDevice.SetRenderTarget(0, zuneTarget);
GraphicsDevice.Viewport = new Viewport
{
X = 0,
Y = 0,
Width = 480,
Height = 272,
MinDepth = GraphicsDevice.Viewport.MinDepth,
MaxDepth = GraphicsDevice.Viewport.MaxDepth
};
}
return val;
}
protected override void EndDraw()
{
GraphicsDevice.SetRenderTarget(0, null);
GraphicsDevice.Viewport = new Viewport
{
X = 0,
Y = 0,
Width = 272,
Height = 480,
MinDepth = GraphicsDevice.Viewport.MinDepth,
MaxDepth = GraphicsDevice.Viewport.MaxDepth
};
GraphicsDevice.Clear(Color.Yellow);
spriteBatch.Begin();
spriteBatch.Draw(
zuneTarget.GetTexture(),
new Vector2(136f, 240f),
null,
Color.White,
MathHelper.PiOver2,
new Vector2(240f, 136f),
1f,
SpriteEffects.None,
0);
spriteBatch.End();
base.EndDraw();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
this.spriteBatch.Begin();
((DrawableWorld)this.engine.World).Render(this.spriteBatch);
this.spriteBatch.End();
base.Draw(gameTime);
}
}
} |