root/Code/oMaE/oMaE/Overworld/Display3D.cs @ 167

Revision 167, 5.8 KB (checked in by ecoughlan, 23 months ago)
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Microsoft.Xna.Framework;
6using Microsoft.Xna.Framework.Content;
7using Microsoft.Xna.Framework.Graphics;
8using Microsoft.Xna.Framework.Input;
9
10namespace oMaE.Overworld
11{
12    using Objects;
13    using MiniGames;
14    using Screens;
15    using Widgets;
16    /// <summary>
17    /// The display for the 3D elements.
18    /// </summary>
19    public class Display3D : GameObject
20    {
21        private static long ARROW_TEXT_LENGTH = 3000;
22        private static long SHIFT_TEXT_LENGTH = 3000;
23        /// <summary>
24        /// A list of all walls on the map
25        /// </summary>
26        private List<Collidable> Walls = new List<Collidable>();
27        /// <summary>
28        /// The User Interface of the overworld.
29        /// </summary>
30        public OverworldUI UI = new OverworldUI();
31        /// <summary>
32        /// The player's elephant.
33        /// </summary>
34        private Elephant you = new Elephant(0, 6, 0);
35        /// <summary>
36        /// Times how long the arrow text has been displayed.
37        /// </summary>
38        private long arrowTextTimer = 0;
39        /// <summary>
40        /// Times how long the shift text has been displayed.
41        /// </summary>
42        private long shiftTextTimer = 0;
43        /// <summary>
44        /// The scale of the 3D world.
45        /// </summary>
46        public readonly float WorldScale = 1.0f;
47        /// <summary>
48        /// The current camera position (current first person).
49        /// </summary>
50        public Vector3 CameraPosition { get { return you.Position; } }
51        /// <summary>
52        /// 2 dimensional camera view.
53        /// </summary>
54        public Vector2 CameraPosition2D { get { return new Vector2(CameraPosition.X, CameraPosition.Z); } }
55        /// <summary>
56        /// The projection matrix.
57        /// </summary>
58        public Matrix ProjectionMatrix { get { return GameDriver.ProjectionMatrix; } }
59        /// <summary>
60        /// The view matrix.
61        /// </summary>
62        public Matrix ViewMatrix { get { return you.ViewMatrix; } }
63        /// <summary>
64        /// Initializes the 3D world.
65        /// </summary>
66        public override void Initialize()
67        {
68            children.Add(you);
69            Walls.AddRange(driver.Map.Walls);
70            // FIXME :
71            //  Should be able just to add range.
72            LinkedList<Door> list = driver.Map.Doors;
73            foreach (Door door in list)
74            {
75                // FIXME :
76                //  This initializations is due to some weird dependency issues
77                //  that need to be worked out at some point if this code
78                //  becomes more complex.
79                door.InitializeAll(driver);
80            }
81            Walls.AddRange(list);
82            Walls.AddRange(driver.Map.Crates);
83            children.Add(new Tile(Color.DarkViolet, new Vector3(400, 0, 400), new Vector3(0, 0, -800), new Vector3(-800, 0, 0), "floor"));
84            children.Add(new Tile(Color.Gray, new Vector3(400, 40, 400), new Vector3(-800, 0, 0), new Vector3(0, 0, -800), "ceiling"));
85            children.AddRange(Walls);
86            children.Add(UI);
87            base.Initialize();
88        }
89        /// <summary>
90        /// Resets the 3D world.
91        /// </summary>
92        public void reset()
93        {
94            arrowTextTimer = 0;
95            you.reset(0, 6, 0);
96        }
97        /// <summary>
98        /// Sets the current location of the camera.
99        /// </summary>
100        /// <param name="loc">Your location.</param>
101        public void SetLocation(Vector2 loc)
102        {
103            you.Position = new Vector3(loc.X, 6, loc.Y);
104        }
105
106        /// <summary>
107        /// Checks for collisions.
108        /// </summary>
109        /// <param name="position">Your current position.</param>
110        /// <param name="delta">Your current velocity change.</param>
111        /// <returns>The required change in velocity to not run into the wall.</returns>
112        public Vector3 Collide(Vector3 position, Vector3 delta)
113        {
114            for (int i = 0; i < 10; ++i )
115            {
116                bool collided = false;
117                foreach (Collidable wall in Walls)
118                    collided |= wall.Collide(position, ref delta);
119                if (!collided)
120                    return delta;
121            }
122            return Vector3.Zero;
123        }
124        /// <summary>
125        /// Checks for nearnesss to an object.
126        /// </summary>
127        /// <param name="position">Your current position.</param>
128        public void Near(Vector3 position)
129        {
130            foreach (Collidable wall in Walls)
131                if (wall.NearText != "" && wall.Near(position))
132                    UI.Text = wall.NearText;
133        }
134        /// <summary>
135        /// Updates the 3D display.
136        /// </summary>
137        /// <param name="gameState">Current gamestate.</param>
138        public override void Update(GameState gameState)
139        {
140            if (arrowTextTimer < ARROW_TEXT_LENGTH)
141            {
142                if(gameState.Down(Keys.Left) || gameState.Down(Keys.Down) || gameState.Down(Keys.Up) || gameState.Down(Keys.Right) || arrowTextTimer > 0)
143                    arrowTextTimer += gameState.EllapsedMilliseconds;
144                UI.Text = "Use the arrow keys to move.";
145            }
146            else if (shiftTextTimer < SHIFT_TEXT_LENGTH)
147            {
148                if(gameState.Down(Keys.LeftShift) || gameState.Down(Keys.RightShift))
149                    shiftTextTimer+= gameState.EllapsedMilliseconds;
150                UI.Text = "Use the shift key to move faster.";
151            }
152            else
153                UI.Text = "";
154            base.Update(gameState);
155        }
156    }
157}
Note: See TracBrowser for help on using the browser.