Jypeli 10
The simple game programming library
Direction.cs
Siirry tämän tiedoston dokumentaatioon.
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
40
41namespace Jypeli
42{
46 public struct Direction
47 {
51 public static Direction None = new Direction( "None", 0, 0 );
52
56 public static Direction Up = new Direction( "Up", 0, 1 );
57
61 public static Direction Down = new Direction( "Down", 0, -1 );
62
66 public static Direction Left = new Direction( "Left", -1, 0 );
67
71 public static Direction Right = new Direction( "Right", 1, 0 );
72
76 private Vector Vector;
77
81 public Angle Angle
82 {
83 get { return Vector.Angle; }
84 }
85
89 public string Name;
90
91 private Direction( string name, int x, int y )
92 {
93 Name = name;
94 Vector = new Vector(x, y);
95 }
96
102 public static Direction Inverse( Direction d )
103 {
104 if ( d == Direction.Up ) return Direction.Down;
105 if ( d == Direction.Down ) return Direction.Up;
106 if ( d == Direction.Left ) return Direction.Right;
107 if ( d == Direction.Right ) return Direction.Left;
108
109 return Direction.None;
110 }
111
117 {
118 return Vector;
119 }
120
125 public override int GetHashCode()
126 {
127 return Name.GetHashCode() ^ Vector.GetHashCode();
128 }
129
135 public override bool Equals( object obj )
136 {
137 if ( !( obj is Direction ) ) return false;
138 return this == ( (Direction)obj );
139 }
140
143 public static bool operator ==(Direction left, Direction right)
144 {
145 return (left.Vector == right.Vector && left.Name == right.Name);
146 }
147
150 public static bool operator !=(Direction left, Direction right)
151 {
152 return (left.Vector != right.Vector || left.Name != right.Name);
153 }
154 }
155}
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40
Perussuunta tasossa.
Definition: Direction.cs:47
Vector Vector
Suuntaa vastaava yksikkövektori.
Definition: Direction.cs:76
string Name
Suunnan nimi.
Definition: Direction.cs:89
static Direction Up
Suunta ylös.
Definition: Direction.cs:56
Direction(string name, int x, int y)
Definition: Direction.cs:91
static bool operator==(Direction left, Direction right)
Definition: Direction.cs:143
static Direction Right
Suunta oikealle.
Definition: Direction.cs:71
override bool Equals(object obj)
Vertaa kahta suuntaa keskenään.
Definition: Direction.cs:135
static Direction Inverse(Direction d)
Palauttaa vastakkaisen suunnan annetulle suunnalle.
Definition: Direction.cs:102
Vector GetVector()
Palauttaa suunnan yksikkövektorina.
Definition: Direction.cs:116
static Direction Down
Suunta alas.
Definition: Direction.cs:61
static Direction Left
Suunta vasemmalle.
Definition: Direction.cs:66
override int GetHashCode()
Serves as a hash function for a particular type.
Definition: Direction.cs:125
static Direction None
Ei suuntaa.
Definition: Direction.cs:51
static bool operator!=(Direction left, Direction right)
Definition: Direction.cs:150
2D-vektori.
Definition: Vector.cs:67
override int GetHashCode()
Vektorin hajautuskoodi
Definition: Vector.cs:438
Angle Angle
Kulma radiaaneina.
Definition: Vector.cs:372