root/Code/oMaE/oMaE/Widgets/Weight.cs @ 119

Revision 119, 6.1 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.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;
12using oMaE.Widgets;
13using oMaE.MiniGames;
14namespace oMaE.Widgets
15{
16    /// <summary>
17    /// Interface for connectors.
18    /// </summary>
19    public interface IConnectable
20    {
21        /// <summary>
22        /// Can the weight connect.
23        /// </summary>
24        /// <param name="loc">Location of connector</param>
25        /// <returns>True if can connect.</returns>
26        bool CanConnect(Vector2 loc);
27        /// <summary>
28        /// Connects the weight to the connector.
29        /// </summary>
30        /// <param name="w"></param>
31        void Connect(Weight w);
32        /// <summary>
33        /// Disconnects the currently connected item.
34        /// </summary>
35        void Disconnect();
36    }
37    /// <summary>
38    /// A weight in the balance minigame.
39    /// </summary>
40    public class Weight : GameObject, IConnectable
41    {
42        Balance parent;
43        private Vector2 location;
44        /// <summary>
45        /// Can the currently draggable object connect here.
46        /// </summary>
47        private bool connecting = false;
48        /// <summary>
49        /// Location of the weight.
50        /// </summary>
51        public Vector2 Location
52        {
53            get { return location; }
54            set
55            {
56                Vector2 delta = value - location;
57                location = value;
58                if (ConnectBottom != null)
59                    ConnectBottom.Location += delta;
60            }
61        }
62        /// <summary>
63        /// The hitbox for this weight.
64        /// </summary>
65        private Rectangle Hitbox
66        {
67            get { return new Rectangle((int)location.X - 50, (int)location.Y, 100, 85); }
68        }
69        /// <summary>
70        /// Connector this is connected to.
71        /// </summary>
72        public IConnectable ConnectTop;
73        /// <summary>
74        /// Connectable that is connected to this.
75        /// </summary>
76        public Weight ConnectBottom;
77        /// <summary>
78        /// Connects to a weight.
79        /// </summary>
80        /// <param name="w">Weight to be connected to.</param>
81        public void Connect(Weight w)
82        {
83            ConnectBottom = w;
84            ConnectBottom.Location = location + new Vector2(0, 55);
85            connecting = false;
86        }
87        /// <summary>
88        /// Disconnects connected weight.
89        /// </summary>
90        public void Disconnect()
91        {
92            ConnectBottom = null;
93        }
94        /// <summary>
95        /// Can the weight connect.
96        /// </summary>
97        /// <param name="loc">Location of connector</param>
98        /// <returns>True if can connect.</returns>
99        public bool CanConnect(Vector2 loc)
100        {
101            return connecting = (ConnectBottom == null && (loc - (location+ new Vector2(0, 55))).Length() < 20);
102        }
103        private bool dragging = false;
104        private bool draggable;
105        /// <summary>
106        /// Value of the weight.
107        /// </summary>
108        public int Worth { get { return weight; } }
109        private int weight;
110        /// <summary>
111        /// Creates a new weight.
112        /// </summary>
113        /// <param name="parent">Minigame the weight is a part of.</param>
114        /// <param name="draggable">Is this weight draggable.</param>
115        /// <param name="x">X-coordinate</param>
116        /// <param name="y">Y-coordinate</param>
117        /// <param name="weight">Value of the weight.</param>
118        public Weight(Balance parent, bool draggable, int x, int y, int weight)
119        {
120            this.parent = parent;
121            this.draggable = draggable;
122            this.weight = weight;
123            location = new Vector2(x, y);
124        }
125        /// <summary>
126        /// Updates the weight.
127        /// </summary>
128        /// <param name="gameState">Current gamestate.</param>
129        public override void Update(GameState gameState)
130        {
131            IConnectable near = null;
132            if (draggable && gameState.LastMouseState.LeftButton == ButtonState.Released
133                          && Hitbox.Contains(gameState.MousePoint)
134                || dragging)
135                gameState.Hover = this;
136            if (Hover && gameState.Pressed(MouseButton.LEFT))
137            {
138                if(ConnectTop != null)
139                    ConnectTop.Disconnect();
140                gameState.Handle(MouseButton.LEFT);
141                dragging = true;
142            }
143            if (dragging)
144            {
145                foreach (IConnectable conn in parent.connectors)
146                    if (conn.CanConnect(location))
147                        near = conn;
148                if (gameState.CurrentMouseState.LeftButton == ButtonState.Released)
149                {
150                    if (near != null)
151                        near.Connect(this);
152                    dragging = false;
153                }
154                else
155                    Location = gameState.MousePos;
156            }
157            base.Update(gameState);
158        }
159        /// <summary>
160        /// Draws the weight.
161        /// </summary>
162        /// <param name="screen">The screen to draw to.</param>
163        public override void Draw(Screen2D screen)
164        {
165            screen.DrawLine(location, location + new Vector2(0, 10), Color.Black);
166            screen.FillRectangle(new Rectangle((int)location.X - 25, (int)location.Y + 10, 50, 50),draggable ? Color.OliveDrab : Color.Maroon);
167            if (connecting)
168                screen.DrawRectangle(new Rectangle((int)location.X - 5, (int)location.Y + 55, 10, 10), Color.White);
169            screen.DrawText(weight.ToString(), location + new Vector2(0, 30), Hover ? FontSize.MEDIUM : FontSize.NORMAL, TextHorizontal.CENTERED, TextVertical.CENTERED, Color.White);
170            base.Draw(screen);
171        }
172    }
173}
Note: See TracBrowser for help on using the browser.