Changeset 15

User picture

Author: conkerjo

(2009/07/01 23:06) Over 2 years ago

Refactoring

Affected files

Updated src/AIDemos/AIDemos.csproj Download diff

1415
89
    <Compile Include="GameClasses\Behaviors\DieBehavior.cs" />
89
    <Compile Include="GameClasses\Behaviors\DieBehavior.cs" />
90
    <Compile Include="GameClasses\GameActor.cs" />
90
    <Compile Include="GameClasses\GameActor.cs" />
91
    <Compile Include="Properties\AssemblyInfo.cs" />
91
    <Compile Include="Properties\AssemblyInfo.cs" />
92
    <Compile Include="Screens\Demos\Demo5.cs" />
92
    <Compile Include="Screens\Demos\Demo4.cs" />
93
    <Compile Include="Screens\Demos\Demo4.cs" />
93
    <Compile Include="Screens\Demos\Demo3.cs" />
94
    <Compile Include="Screens\Demos\Demo3.cs" />
94
    <Compile Include="Screens\Demos\Demo2.cs" />
95
    <Compile Include="Screens\Demos\Demo2.cs" />

Updated src/AIDemos/Content/Content.contentproj Download diff

1415
94
      <Processor>TextureProcessor</Processor>
94
      <Processor>TextureProcessor</Processor>
95
    </Compile>
95
    </Compile>
96
  </ItemGroup>
96
  </ItemGroup>
97
  <ItemGroup>
98
    <Compile Include="DemoTextures\MAPDemoBlank8.png">
99
      <Name>MAPDemoBlank8</Name>
100
      <Importer>TextureImporter</Importer>
101
      <Processor>TextureProcessor</Processor>
102
    </Compile>
103
  </ItemGroup>
97
  <Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets"
/>
104
  <Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets"
/>
98
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
105
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
99
       Other similar extension points exist, see Microsoft.Common.targets.
106
       Other similar extension points exist, see Microsoft.Common.targets.

Updated src/AIDemos/GameClasses/GameActor.cs Download diff

1415
17
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
17
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
18
        {
18
        {
19
            base.Update(gameTime);
19
            base.Update(gameTime);
20
            WrapAround();
20
        }
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
        }
21
    }
40
    }
22
}
41
}

Updated src/AIDemos/Screens/Demos/Demo1.cs Download diff

1415
14
using Brains;
14
using Brains;
15
using Brains.Framework.Behaviors;
15
using Brains.Framework.Behaviors;
16
using AIDemos.GameClasses.Behaviors;
16
using AIDemos.GameClasses.Behaviors;
17
using Brains.Framework.Map;
18
using Brains.Framework.Utility;
17
19
18
namespace AIDemos.Screens.Demos
20
namespace AIDemos.Screens.Demos
19
{
21
{
...
...
46
48
47
            primitiveBatch.CameraPosition = new Vector2(
49
            primitiveBatch.CameraPosition = new Vector2(
48
           -ScreenManager.GraphicsDevice.Viewport.Width / 2 +
50
           -ScreenManager.GraphicsDevice.Viewport.Width / 2 +
49
                       _engine.World.Map.Cluster[0].Width / 2,
51
                       _engine.World.Map.ClusterGrid.Grids[0].Width / 2,
50
           -ScreenManager.GraphicsDevice.Viewport.Height / 2 +
52
           -ScreenManager.GraphicsDevice.Viewport.Height / 2 +
51
                       _engine.World.Map.Cluster[0].Height / 2);
53
                       _engine.World.Map.ClusterGrid.Grids[0].Height / 2);
52
       
54
       
53
55
54
            ScreenManager.Game.ResetElapsedTime();
56
            ScreenManager.Game.ResetElapsedTime();
...
...
118
                    _lastMouse.LeftButton == ButtonState.Pressed)
120
                    _lastMouse.LeftButton == ButtonState.Pressed)
119
                {
121
                {
120
                    Vector2 mapMousePos = mousPos + primitiveBatch.CameraPosition;
122
                    Vector2 mapMousePos = mousPos + primitiveBatch.CameraPosition;
121
                    GridCell cell = _engine.World.Map.Cluster[0].GetCellAtPosition(mapMousePos);
123
                    GridCell cell = _engine.World.Map.ClusterGrid.Grids[0].GetCellAtPosition(mapMousePos);
122
                    if (cell != null)
124
                    if (cell != null)
123
                    {
125
                    {
124
                        GameActor actor = new GameActor(cell.Position, 5);
126
                        GameActor actor = new GameActor(cell.Position, 5);

Updated src/AIDemos/Screens/Demos/Demo2.cs Download diff

1415
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.Map;
19
using Brains.Framework.Utility;
18
20
19
namespace AIDemos.Screens.Demos
21
namespace AIDemos.Screens.Demos
20
{
22
{
...
...
41
43
42
            primitiveBatch.CameraPosition = new Vector2(
44
            primitiveBatch.CameraPosition = new Vector2(
43
            -ScreenManager.GraphicsDevice.Viewport.Width / 2 +
45
            -ScreenManager.GraphicsDevice.Viewport.Width / 2 +
44
                        _engine.World.Map.Cluster[0].Width / 2,
46
                        _engine.World.Map.ClusterGrid.Grids[0].Width / 2,
45
            -ScreenManager.GraphicsDevice.Viewport.Height / 2 +
47
            -ScreenManager.GraphicsDevice.Viewport.Height / 2 +
46
                        _engine.World.Map.Cluster[0].Height / 2);
48
                        _engine.World.Map.ClusterGrid.Grids[0].Height / 2);
47
        
49
        
48
            ScreenManager.Game.ResetElapsedTime();
50
            ScreenManager.Game.ResetElapsedTime();
49
        }
51
        }
...
...
154
                    _lastMouse.LeftButton == ButtonState.Pressed)
156
                    _lastMouse.LeftButton == ButtonState.Pressed)
155
                {
157
                {
156
                    Vector2 mapMousePos = mousPos + primitiveBatch.CameraPosition;
158
                    Vector2 mapMousePos = mousPos + primitiveBatch.CameraPosition;
157
                    GridCell cell = _engine.World.Map.Cluster[0].GetCellAtPosition(mapMousePos);
159
                    GridCell cell = _engine.World.Map.ClusterGrid.Grids[0].GetCellAtPosition(mapMousePos);
158
                    if (cell != null)
160
                    if (cell != null)
159
                    {
161
                    {
160
                        GameActor actor = new GameActor(cell.Position, 5);
162
                        GameActor actor = new GameActor(cell.Position, 5);

Updated src/AIDemos/Screens/Demos/Demo3.cs Download diff

1415
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.Map;
19
using Brains.Framework.Utility;
18
20
19
namespace AIDemos.Screens.Demos
21
namespace AIDemos.Screens.Demos
20
{
22
{

Updated src/AIDemos/Screens/Demos/Demo4.cs Download diff

1415
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);

Added src/AIDemos/Screens/Demos/Demo5.cs

Show contents

Updated src/AIDemos/Screens/MainMenuScreen.cs Download diff

1415
19
    /// </summary>
19
    /// </summary>
20
    class MainMenuScreen : MenuScreen
20
    class MainMenuScreen : MenuScreen
21
    {
21
    {
22
        #region Initialization
22
        
23
23
24
24
25
        /// <summary>
25
        /// <summary>
...
...
41
            playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
41
            playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
42
            MenuEntries.Add(playGameMenuEntry);
42
            MenuEntries.Add(playGameMenuEntry);
43
43
44
            playGameMenuEntry = new MenuEntry("Demo4 - A Bigger World");
44
            playGameMenuEntry = new MenuEntry("Demo4 - Steering Motion Controller");
45
            playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
45
            playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
46
            MenuEntries.Add(playGameMenuEntry);
46
            MenuEntries.Add(playGameMenuEntry);
47
            
47
            
...
...
60
        }
60
        }
61
61
62
62
63
        #endregion
64
63
65
        #region Handle Input
64
     
66
65
67
68
        /// <summary>
66
        /// <summary>
69
        /// Event handler for when the Play Game menu entry is selected.
67
        /// Event handler for when the Play Game menu entry is selected.
70
        /// </summary>
68
        /// </summary>
...
...
84
                    LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
82
                    LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
85
                               new Demo3());
83
                               new Demo3());
86
                    break;
84
                    break;
87
                case "Demo4 - A Bigger World":
85
                case "Demo4 - Steering Motion Controller":
88
                    LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
86
                    LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
89
                               new Demo4());
87
                               new Demo4());
90
                    break;
88
                    break;
89
                case "Demo5 - A Bigger World":
90
                    LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
91
                               new Demo5());
92
                    break;
91
            }
93
            }
92
        }
94
        }
93
95
...
...
126
        }
128
        }
127
129
128
130
129
        #endregion
130
    }
131
    }
131
}
132
}