root/Code/oMaE/oMaE/Overworld/Objects/Door.cs @ 58

Revision 58, 5.6 KB (checked in by acarter, 2 years ago)

Match game always clears

Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Microsoft.Xna.Framework;
6using Microsoft.Xna.Framework.Audio;
7using Microsoft.Xna.Framework.Content;
8using Microsoft.Xna.Framework.GamerServices;
9using Microsoft.Xna.Framework.Graphics;
10using Microsoft.Xna.Framework.Input;
11using Microsoft.Xna.Framework.Media;
12
13namespace oMaE.Overworld.Objects
14{
15    using MiniGames;
16    /// <summary>
17    /// A 3D door.
18    /// </summary>
19    public class Door : Collidable
20    {
21        private Vector2 otherSide;
22        public MiniGame DoorLock;
23        private Tile door, wall;
24        private bool elephantNear;
25        private String nearText;
26        private Color completed;
27        private Door partner = null;
28        /// <summary>
29        /// The text to be displayed for being near the door.
30        /// </summary>
31        public override string NearText { get { return nearText; }}
32        /// <summary>
33        /// Creates a new door.
34        /// </summary>
35        /// <param name="doorColor">The color of the door.</param>
36        /// <param name="doorColor2">The color of the door when completed.</param>
37        /// <param name="wallColor">The color of the wall above the door.</param>
38        /// <param name="left">The left side of the door.</param>
39        /// <param name="right">The right side of the door.</param>
40        /// <param name="doorHeight">The height of the door.</param>
41        /// <param name="wallHeight">The width of the door.</param>
42        /// <param name="nearText">The text of the door when nearby.</param>
43        public Door(Color doorColor, Color doorColor2, Color wallColor,
44                    Vector2 left, Vector2 right,
45                    float doorHeight = 8.0f, float wallHeight = 10.0f, String nearText = "Press the space button to open the door.")
46        {
47            right -= left;
48            Vector2 normal = new Vector2(right.Y, -right.X);
49            normal.Normalize();
50            otherSide = left + 0.5f * right - 5.0f * normal;
51            wallHeight -= doorHeight;
52            completed = doorColor2;
53            door = new Tile(doorColor, new Vector3(left.X, 0, left.Y), new Vector3(0, doorHeight, 0), new Vector3(right.X, 0, right.Y));
54            wall = new Tile(wallColor, new Vector3(left.X, doorHeight, left.Y), new Vector3(0, wallHeight, 0), new Vector3(right.X, 0, right.Y));
55            this.nearText = nearText;
56        }
57        public Door(Color doorColor, Color doorColor2, Color wallColor,
58                    Vector2 left, Vector2 right, Door partner,
59                    float doorHeight = 8.0f, float wallHeight = 10.0f, String nearText = "Press the space button to open the door.")
60        {
61            this.partner = partner;
62            partner.partner = this;
63            right -= left;
64            Vector2 normal = new Vector2(right.Y, -right.X);
65            normal.Normalize();
66            otherSide = left + 0.5f * right - 5.0f * normal;
67            wallHeight -= doorHeight;
68            completed = doorColor2;
69            door = new Tile(doorColor, new Vector3(left.X, 0, left.Y), new Vector3(0, doorHeight, 0), new Vector3(right.X, 0, right.Y));
70            wall = new Tile(wallColor, new Vector3(left.X, doorHeight, left.Y), new Vector3(0, wallHeight, 0), new Vector3(right.X, 0, right.Y));
71            this.nearText = nearText;
72        }
73        /// <summary>
74        /// Initializes the door.
75        /// </summary>
76        public override void Initialize()
77        {
78            children.Add(door);
79            children.Add(wall);
80            // FIXME
81            if (partner == null || partner.DoorLock == null)
82                DoorLock = driver.GetMinigame();
83            else
84                DoorLock = partner.DoorLock;
85            base.Initialize();
86        }
87        /// <summary>
88        /// Checks for a collision.
89        /// </summary>
90        /// <param name="position">The position of the object.</param>
91        /// <param name="delta">The velocity of the object.</param>
92        /// <returns>Whether they collided.</returns>
93        public override bool Collide(Vector3 position, ref Vector3 delta)
94        {
95            Vector3 unitDelta = new Vector3(delta.X, delta.Y, delta.Z);
96            unitDelta.Normalize();
97            float where = door.Bounds.Intersects(new Ray(position, unitDelta)) ?? float.PositiveInfinity;
98            if (where > 0 && where < delta.Length())
99            {
100                delta -= 2 * Vector3.Dot(delta, door.Normal) * door.Normal;
101                return true;
102            }
103            where = wall.Bounds.Intersects(new Ray(position, unitDelta)) ?? float.PositiveInfinity;
104            if (where > 0 && where < delta.Length())
105            {
106                delta -= 2 * Vector3.Dot(delta, wall.Normal) * wall.Normal;
107                return true;
108            }
109            return false;
110        }
111        /// <summary>
112        /// Updates the door.
113        /// </summary>
114        /// <param name="gameState"></param>
115        public override void Update(GameState gameState)
116        {
117            if (elephantNear && gameState.Down(Keys.Space))
118                driver.Play2DGame(DoorLock, otherSide);
119            if (DoorLock.Completed)
120                door.Color = completed;
121            base.Update(gameState);
122        }
123        /// <summary>
124        /// Is the elephant near the door.
125        /// </summary>
126        /// <param name="position"></param>
127        /// <returns></returns>
128        public override bool Near(Vector3 position)
129        {
130            return (elephantNear = door.Near.Contains(position) == ContainmentType.Contains);
131        }
132    }
133}
Note: See TracBrowser for help on using the browser.