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

Revision 102, 5.4 KB (checked in by acarter, 2 years ago)

Finished working parts of Binary Builder.

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        /// Location of the weight.
46        /// </summary>
47        public Vector2 Location
48        {
49            get { return location; }
50            set
51            {
52                Vector2 delta = value - location;
53                location = value;
54                if (ConnectBottom != null)
55                    ConnectBottom.Location += delta;
56            }
57        }
58        /// <summary>
59        /// Connector this is connected to.
60        /// </summary>
61        public IConnectable ConnectTop;
62        /// <summary>
63        /// Connectable that is connected to this.
64        /// </summary>
65        public Weight ConnectBottom;
66        /// <summary>
67        /// Connects to a weight.
68        /// </summary>
69        /// <param name="w">Weight to be connected to.</param>
70        public void Connect(Weight w)
71        {
72            ConnectBottom = w;
73            ConnectBottom.Location = location + new Vector2(0, 55);
74        }
75        /// <summary>
76        /// Disconnects connected weight.
77        /// </summary>
78        public void Disconnect()
79        {
80            ConnectBottom = null;
81        }
82        /// <summary>
83        /// Can the weight connect.
84        /// </summary>
85        /// <param name="loc">Location of connector</param>
86        /// <returns>True if can connect.</returns>
87        public bool CanConnect(Vector2 loc)
88        {
89            return ConnectBottom == null && (loc - (location+ new Vector2(0, 55))).Length() < 20;
90        }
91        private bool dragging = false;
92        private bool draggable;
93        /// <summary>
94        /// Value of the weight.
95        /// </summary>
96        public int Worth { get { return weight; } }
97        private int weight;
98        /// <summary>
99        /// Creates a new weight.
100        /// </summary>
101        /// <param name="parent">Minigame the weight is a part of.</param>
102        /// <param name="draggable">Is this weight draggable.</param>
103        /// <param name="x">X-coordinate</param>
104        /// <param name="y">Y-coordinate</param>
105        /// <param name="weight">Value of the weight.</param>
106        public Weight(Balance parent, bool draggable, int x, int y, int weight)
107        {
108            this.parent = parent;
109            this.draggable = draggable;
110            this.weight = weight;
111            location = new Vector2(x, y);
112        }
113        /// <summary>
114        /// Updates the weight.
115        /// </summary>
116        /// <param name="gameState">Current gamestate.</param>
117        public override void Update(GameState gameState)
118        {
119            if (draggable && gameState.Pressed(MouseButton.LEFT) && new Rectangle((int)location.X - 50, (int)location.Y, 100, 85).Contains(gameState.MousePoint))
120            {
121                if(ConnectTop != null)
122                    ConnectTop.Disconnect();
123                gameState.Handle(MouseButton.LEFT);
124                dragging = true;
125            }
126            if (dragging && gameState.CurrentMouseState.LeftButton == ButtonState.Released)
127            {
128                foreach(IConnectable conn in parent.connectors)
129                {
130                    if (conn.CanConnect(location))
131                    {
132                        ConnectTop = conn;
133                        conn.Connect(this);
134                        break;
135                    }
136                }
137                dragging = false;
138            }
139            if (dragging)
140                Location = gameState.MousePos;
141            base.Update(gameState);
142        }
143        /// <summary>
144        /// Draws the weight.
145        /// </summary>
146        /// <param name="screen">The screen to draw to.</param>
147        public override void Draw(Screen2D screen)
148        {
149            screen.DrawLine(location, location + new Vector2(0, 10), Color.Black);
150            screen.FillRectangle(new Rectangle((int)location.X - 25, (int)location.Y + 10, 50, 50),draggable ? Color.OliveDrab : Color.Maroon);
151            screen.DrawText(weight.ToString(), location + new Vector2(0, 30), TextHorizontal.CENTERED, TextVertical.CENTERED, Color.White);
152            base.Draw(screen);
153        }
154    }
155}
Note: See TracBrowser for help on using the browser.