Jypeli  9
The simple game programming library
Movement.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 using System;
31 using Jypeli.GameObjects;
32 
33 namespace Jypeli
34 {
35  public partial class GameObject
36  {
38 
39  protected Timer moveTimer = null;
40  protected Vector? moveTarget = null;
41  protected double moveSpeed;
42  protected Action arrivedAction = null;
43 
48  public virtual void Move( Vector movement )
49  {
50  Position += movement;
51  }
52 
67  public override void MoveTo( Vector location, double speed, Action doWhenArrived )
68  {
69  if ( moveTimer == null )
70  {
71  moveTimer = new Timer();
73  moveTimer.Interval = 0.01;
74  }
75  else if ( moveTimer.Enabled )
76  moveTimer.Stop();
77 
78  moveSpeed = speed;
79  moveTarget = location;
80  arrivedAction = doWhenArrived;
81  moveTimer.Start();
82  }
83 
87  public void StopMoveTo()
88  {
89  if ( moveTimer != null )
90  {
91  moveTimer.Stop();
92  moveTarget = null;
93  }
94  }
95 
96  protected virtual void MoveToTarget()
97  {
98  if ( !moveTarget.HasValue )
99  {
100  moveTimer.Stop();
101  return;
102  }
103 
104  Vector d = moveTarget.Value - AbsolutePosition;
105  double vt = moveSpeed * moveTimer.Interval;
106 
107  if ( d.Magnitude < vt )
108  {
109  Vector targetLoc = moveTarget.Value;
110  moveTimer.Stop();
111  AbsolutePosition = moveTarget.Value;
112  moveTarget = null;
113 
114  if ( arrivedAction != null )
115  arrivedAction();
116  }
117  else
118  {
119  AbsolutePosition += Vector.FromLengthAndAngle( vt, d.Angle );
120  }
121  }
122 
131  public void Oscillate( Vector axis, double amplitude, double frequency, double phase = 0, double damping = 0 )
132  {
133  if ( oscillators == null )
135 
136  oscillators.Add( new LinearOscillator( this, axis, amplitude, frequency, phase, damping ) );
137  IsUpdated = true;
138  }
139 
147  public void OscillateAngle( double direction, UnlimitedAngle amplitude, double frequency, double damping = 0 )
148  {
149  if ( oscillators == null )
151 
152  oscillators.Add( new AngularOscillator( this, direction, amplitude, frequency, damping ) );
153  IsUpdated = true;
154  }
155 
159  public void SetEquilibrium()
160  {
161  throw new NotImplementedException();
162  }
163 
167  public void ClearOscillations()
168  {
169  oscillators = null;
170  }
171 
175  public virtual void Stop()
176  {
177  StopMoveTo();
179  }
180  }
181 }
Jypeli.Timer.Enabled
bool Enabled
Ajastin päällä/pois päältä.
Definition: Timer.cs:66
Jypeli.GameObject.Move
virtual void Move(Vector movement)
Siirtää oliota.
Definition: Movement.cs:48
Jypeli.GameObject.MoveTo
override void MoveTo(Vector location, double speed, Action doWhenArrived)
Yrittää siirtyä annettuun paikkaan annetulla nopeudella. Laukaisee annetun aliohjelman,...
Definition: Movement.cs:67
Jypeli.GameObject.Oscillate
void Oscillate(Vector axis, double amplitude, double frequency, double phase=0, double damping=0)
Laittaa kappaleen värähtelemään edestakaisin nykyisen paikkansa ympärillä tietyn akselin suuntaisesti...
Definition: Movement.cs:131
Jypeli.GameObject.Stop
virtual void Stop()
Pysäyttää kaiken liikkeen.
Definition: Movement.cs:175
Jypeli.GameObject.StopMoveTo
void StopMoveTo()
Pysäyttää MoveTo-aliohjelmalla aloitetun liikkeen.
Definition: Movement.cs:87
Jypeli.Timer.Timeout
Action Timeout
Tapahtuu väliajoin.
Definition: Timer.cs:44
Jypeli
Definition: Automobile.cs:5
Jypeli.GameObject.oscillators
SynchronousList< Oscillator > oscillators
Definition: Movement.cs:37
Jypeli.UnlimitedAngle
Rajoittamaton kulma (asteina ja radiaaneina). Tietoja kulmasta: http://en.wikipedia....
Definition: UnlimitedAngle.cs:40
Jypeli.GameObjects.LinearOscillator
Harmoninen värähtelijä akselin suhteen.
Definition: Oscillator.cs:63
Jypeli.SynchronousList.Add
void Add(T item)
Definition: SynchronousList.cs:197
Jypeli.GameObjects
Definition: GameObjectBase.cs:5
Jypeli.GameObject.arrivedAction
Action arrivedAction
Definition: Movement.cs:42
Jypeli.GameObject.moveSpeed
double moveSpeed
Definition: Movement.cs:41
Jypeli.GameObjects.AngularOscillator
Harmoninen värähtelijä pyörintäliikkeelle.
Definition: Oscillator.cs:124
Jypeli.GameObject.ClearOscillations
void ClearOscillations()
Poistaa kaikki värähtelyt kappaleelta.
Definition: Movement.cs:167
Jypeli.Timer.Interval
double Interval
Aika sekunneissa, jonka välein TimeOut tapahtuu.
Definition: Timer.cs:87
Jypeli.GameObject.MoveToTarget
virtual void MoveToTarget()
Definition: Movement.cs:96
Jypeli.GameObject.SetEquilibrium
void SetEquilibrium()
Asettaa uuden tasapainoaseman värähtelyille.
Definition: Movement.cs:159
Jypeli.Vector.Angle
Angle Angle
Kulma radiaaneina.
Definition: Vector.cs:346
Jypeli.Timer.Stop
void Stop()
Pysäyttää ajastimen ja nollaa sen tilan.
Definition: Timer.cs:292
Jypeli.GameObject.moveTimer
Timer moveTimer
Definition: Movement.cs:39
Jypeli.Vector.FromLengthAndAngle
static Vector FromLengthAndAngle(double length, double angle)
Luo vektorin pituuden ja kulman perusteella.
Definition: Vector.cs:106
Jypeli.Vector.Magnitude
double Magnitude
Vektorin pituus.
Definition: Vector.cs:319
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
System
Definition: CFFauxAttributes.cs:29
Jypeli.Timer
Ajastin, joka voidaan asettaa laukaisemaan tapahtumia tietyin väliajoin.
Definition: Timer.cs:38
Jypeli.GameObject.moveTarget
Vector? moveTarget
Definition: Movement.cs:40
Jypeli.SynchronousList< Oscillator >
Jypeli.GameObject.OscillateAngle
void OscillateAngle(double direction, UnlimitedAngle amplitude, double frequency, double damping=0)
Laittaa kappaleen kulman värähtelemään edestakaisin.
Definition: Movement.cs:147
Jypeli.Timer.Start
void Start()
Käynnistää ajastimen.
Definition: Timer.cs:257