source: 2013/30/MiskaK/MW2(My Warfare 2)/Paranneltu Jypeli/DataTypes/Direction.cs @ 4507

Revision 4507, 4.2 KB checked in by anlakane, 6 years ago (diff)

Talletus.

Line 
1#region MIT License
2/*
3 * Copyright (c) 2009 University of Jyväskylä, Department of Mathematical
4 * Information Technology.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#endregion
25
26/*
27 * Authors: Tero Jäntti, Tomi Karppinen, Janne Nikkanen.
28 */ 
29
30/*
31 * Directions
32 * 
33 * 14.4.2009 (Tomi Karppinen)
34 *    - Class is now serializable
35 * 14.4.2009 (Tomi Karppinen)
36 *    - Added the Direction enumeration
37 * 14.4.2009 Initial version (Tomi Karppinen)
38 */
39
40using System;
41using System.Collections.Generic;
42using AdvanceMath;
43using Physics2DDotNet;
44
45namespace Jypeli
46{
47    /// <summary>
48    /// Perussuunta tasossa.
49    /// </summary>
50    public struct Direction
51    {
52        /// <summary>
53        /// Ei suuntaa.
54        /// </summary>
55        public static Direction None = new Direction( "None", 0, 0 );
56
57        /// <summary>
58        /// Suunta ylös.
59        /// </summary>
60        public static Direction Up = new Direction( "Up", 0, 1 );
61
62        /// <summary>
63        /// Suunta alas.
64        /// </summary>
65        public static Direction Down = new Direction( "Down", 0, -1 );
66
67        /// <summary>
68        /// Suunta vasemmalle.
69        /// </summary>
70        public static Direction Left = new Direction( "Left", -1, 0 );
71
72        /// <summary>
73        /// Suunta oikealle.
74        /// </summary>
75        public static Direction Right = new Direction( "Right", 1, 0 );
76
77        /// <summary>
78        /// Suuntaa vastaava yksikkövektori.
79        /// </summary>
80        private Vector Vector;
81
82        /// <summary>
83        /// Suuntaa vastaava kulma.
84        /// </summary>
85        public Angle Angle
86        {
87            get { return Vector.Angle; }
88        }
89
90        /// <summary>
91        /// Suunnan nimi.
92        /// </summary>
93        public string Name;
94
95        private Direction( string name, int x, int y )
96        {           
97            Name = name;
98            Vector = new Vector(x, y);
99        }
100
101        /// <summary>
102        /// Palauttaa vastakkaisen suunnan annetulle suunnalle.
103        /// </summary>
104        /// <param name="d">Suunta.</param>
105        /// <returns>Vastakkainen suunta.</returns>
106        public static Direction Inverse( Direction d )
107        {
108            if ( d == Direction.Up ) return Direction.Down;
109            if ( d == Direction.Down ) return Direction.Up;
110            if ( d == Direction.Left ) return Direction.Right;
111            if ( d == Direction.Right ) return Direction.Left;
112
113            return Direction.None;
114        }
115
116        public Vector GetVector()
117        {
118            return Vector;
119        }
120
121        public override bool Equals( object obj )
122        {
123            if ( !( obj is Direction ) ) return false;
124            return this == ( (Direction)obj );
125        }
126
127        public static bool operator ==(Direction left, Direction right)
128        {
129            return (left.Vector == right.Vector && left.Name == right.Name);
130        }
131
132        public static bool operator !=(Direction left, Direction right)
133        {
134            return (left.Vector != right.Vector || left.Name != right.Name);
135        }
136    }
137}
Note: See TracBrowser for help on using the repository browser.