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

Revision 119, 5.7 KB (checked in by acarter, 2 years ago)

Committed in semi-stable state

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        public bool[] Completed;
64        /// <summary>
65        /// Initializes the 3D world.
66        /// </summary>
67        public override void Initialize()
68        {
69            children.Add(you);
70            Walls.AddRange(driver.Map.Walls);
71            // FIXME
72            LinkedList<Door> list = driver.Map.Doors;
73            int i = 0, j = 0;
74            foreach (Door door in list)
75            {
76                // FIXME
77                door.InitializeAll(driver);
78                door.DoorLock.Completed = Completed[i];
79                i += j;
80                j = 1 - j;
81            }
82            Walls.AddRange(list);
83            Walls.AddRange(driver.Map.Crates);
84            children.Add(new Tile(Color.DarkViolet, new Vector3(400, 0, 400), new Vector3(0, 0, -800), new Vector3(-800, 0, 0)));
85            children.Add(new Tile(Color.Gray, new Vector3(400, 40, 400), new Vector3(-800, 0, 0), new Vector3(0, 0, -800)));
86            children.AddRange(Walls);
87            children.Add(UI);
88            base.Initialize();
89        }
90        /// <summary>
91        /// Resets the 3D world.
92        /// </summary>
93        public void reset()
94        {
95            arrowTextTimer = 0;
96            you.reset(0, 6, 0);
97        }
98        /// <summary>
99        /// Sets the current location of the camera.
100        /// </summary>
101        /// <param name="loc">Your location.</param>
102        public void SetLocation(Vector2 loc)
103        {
104            you.Position = new Vector3(loc.X, 6, loc.Y);
105        }
106
107        /// <summary>
108        /// Checks for collisions.
109        /// </summary>
110        /// <param name="position">Your current position.</param>
111        /// <param name="delta">Your current velocity change.</param>
112        /// <returns>The required change in velocity to not run into the wall.</returns>
113        public Vector3 Collide(Vector3 position, Vector3 delta)
114        {
115            for (int i = 0; i < 10; ++i )
116            {
117                bool collided = false;
118                foreach (Collidable wall in Walls)
119                    collided |= wall.Collide(position, ref delta);
120                if (!collided)
121                    return delta;
122            }
123            return Vector3.Zero;
124        }
125        /// <summary>
126        /// Checks for nearnesss to an object.
127        /// </summary>
128        /// <param name="position">Your current position.</param>
129        public void Near(Vector3 position)
130        {
131            foreach (Collidable wall in Walls)
132                if (wall.NearText != "" && wall.Near(position))
133                    UI.Text = wall.NearText;
134        }
135        /// <summary>
136        /// Updates the 3D display.
137        /// </summary>
138        /// <param name="gameState">Current gamestate.</param>
139        public override void Update(GameState gameState)
140        {
141            if (arrowTextTimer < ARROW_TEXT_LENGTH)
142            {
143                if(gameState.Down(Keys.A) || gameState.Down(Keys.S) || gameState.Down(Keys.W) || gameState.Down(Keys.D) || arrowTextTimer > 0)
144                    arrowTextTimer += gameState.EllapsedMilliseconds;
145                UI.Text = "Use W to move forward, S to move backwards, and A and D to turn.";
146            }
147            else if (shiftTextTimer < SHIFT_TEXT_LENGTH)
148            {
149                if(gameState.Down(Keys.LeftShift) || gameState.Down(Keys.RightShift))
150                    shiftTextTimer+= gameState.EllapsedMilliseconds;
151                UI.Text = "Use the shift key to move faster.";
152            }
153            else
154                UI.Text = "";
155            base.Update(gameState);
156        }
157    }
158}
Note: See TracBrowser for help on using the browser.