Jypeli  5
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 System.Collections.Generic;
32 using System.Linq;
33 using System.Text;
34 using Jypeli.GameObjects;
35 
36 namespace Jypeli
37 {
38  public partial class GameObject
39  {
40  protected Timer moveTimer = null;
41  protected Vector? moveTarget = null;
42  protected double moveSpeed;
43  protected Action arrivedAction = null;
44 
45  private SynchronousList<Oscillator> oscillators = null;
46 
51  public virtual void Move( Vector movement )
52  {
53  Position += movement;
54  }
55 
70  public override void MoveTo( Vector location, double speed, Action doWhenArrived )
71  {
72  if ( moveTimer == null )
73  {
74  moveTimer = new Timer();
75  moveTimer.Timeout += MoveToTarget;
76  moveTimer.Interval = 0.01;
77  }
78  else if ( moveTimer.Enabled )
79  moveTimer.Stop();
80 
81  moveSpeed = speed;
82  moveTarget = location;
83  arrivedAction = doWhenArrived;
84  moveTimer.Start();
85  }
86 
90  public void StopMoveTo()
91  {
92  if ( moveTimer != null )
93  {
94  moveTimer.Stop();
95  moveTarget = null;
96  }
97  }
98 
99  protected virtual void MoveToTarget()
100  {
101  if ( !moveTarget.HasValue )
102  {
103  moveTimer.Stop();
104  return;
105  }
106 
107  Vector d = moveTarget.Value - AbsolutePosition;
108  double vt = moveSpeed * moveTimer.Interval;
109 
110  if ( d.Magnitude < vt )
111  {
112  Vector targetLoc = moveTarget.Value;
113  moveTimer.Stop();
114  AbsolutePosition = moveTarget.Value;
115  moveTarget = null;
116 
117  if ( arrivedAction != null )
118  arrivedAction();
119  }
120  else
121  {
122  AbsolutePosition += Vector.FromLengthAndAngle( vt, d.Angle );
123  }
124  }
125 
126  private void AssertOscillators()
127  {
128  if ( oscillators == null )
129  oscillators = new SynchronousList<Oscillator>();
130 
131  IsUpdated = true;
132  }
133 
144  public void Oscillate<WaveForm>( Vector axis, double amplitude, double frequency, double phase = 0, double damping = 0 ) where WaveForm : Waveform
145  {
146  AssertOscillators();
147 
148  Waveform waveform = (Waveform)Activator.CreateInstance( typeof( WaveForm ), new object[] { amplitude, frequency, phase, damping } );
149  oscillators.Add( new LinearOscillator( this, axis, waveform ) );
150  }
151 
160  public void Oscillate( Vector axis, double amplitude, double frequency, double phase = 0, double damping = 0 )
161  {
162  AssertOscillators();
163 
164  Waveform waveform = new Waveform.Sine( amplitude, frequency, phase, damping );
165  oscillators.Add( new LinearOscillator( this, axis, waveform ) );
166  }
167 
177  public void OscillateAngle<WaveForm>( double direction, UnlimitedAngle amplitude, double frequency, double damping = 0 ) where WaveForm : Waveform
178  {
179  AssertOscillators();
180 
181  Waveform waveform = (Waveform)Activator.CreateInstance( typeof( WaveForm ), new object[] { amplitude.Radians, frequency, 0, damping } );
182  oscillators.Add( new AngularOscillator( this, direction, waveform ) );
183  }
184 
192  public void OscillateAngle( double direction, UnlimitedAngle amplitude, double frequency, double damping = 0 )
193  {
194  AssertOscillators();
195 
196  Waveform waveform = new Waveform.Sine( amplitude.Radians, frequency, 0, damping );
197  oscillators.Add( new AngularOscillator( this, direction, waveform ) );
198  }
199 
203  public void SetEquilibrium()
204  {
205  throw new NotImplementedException();
206  }
207 
211  public void ClearOscillations()
212  {
213  oscillators = null;
214  }
215 
219  public virtual void Stop()
220  {
221  StopMoveTo();
223  }
224  }
225 }
void ClearOscillations()
Poistaa kaikki värähtelyt kappaleelta.
Definition: Movement.cs:211
Aaltomuoto.
Definition: Waveform.cs:11
double Magnitude
Vektorin pituus.
Definition: Vector.cs:281
override void MoveTo(Vector location, double speed, Action doWhenArrived)
Yrittää siirtyä annettuun paikkaan annetulla nopeudella. Laukaisee annetun aliohjelman, kun paikkaan on päästy.
Definition: Movement.cs:70
Angle Angle
Kulma radiaaneina.
Definition: Vector.cs:308
virtual void MoveToTarget()
Action arrivedAction
Definition: Movement.cs:43
Siniaalto.
Definition: Waveform.cs:16
void SetEquilibrium()
Asettaa uuden tasapainoaseman värähtelyille.
Definition: Movement.cs:203
void OscillateAngle< WaveForm >(double direction, UnlimitedAngle amplitude, double frequency, double damping=0)
Laittaa kappaleen pyörimään edestakaisin tietyn aaltomuodon mukaisesti. Aaltomuotoa voi vaihtaa tyyppip...
Definition: Movement.cs:177
void Oscillate(Vector axis, double amplitude, double frequency, double phase=0, double damping=0)
Laittaa kappaleen liikkumaan edestakaisin nykyisen paikkansa ympärillä tietyn akselin suuntaisesti...
Definition: Movement.cs:160
bool IsUpdated
Tarvitseeko olio päivittämistä. Kun perit oman luokkasi tästä luokasta, aseta tämä arvoon true...
virtual void Move(Vector movement)
Siirtää oliota.
Definition: Movement.cs:51
void Oscillate< WaveForm >(Vector axis, double amplitude, double frequency, double phase=0, double damping=0)
Laittaa kappaleen liikkumaan edestakaisin nykyisen paikkansa ympärillä tietyn akselin suuntaisesti...
Definition: Movement.cs:144
virtual void Stop()
Pysäyttää kaiken liikkeen.
Definition: Movement.cs:219
Ajastin, joka voidaan asettaa laukaisemaan tapahtumia tietyin väliajoin.
Definition: Timer.cs:39
Rajoittamaton kulma (asteina ja radiaaneina). Tietoja kulmasta: http://en.wikipedia.org/wiki/File:Degree-Radian_Conversion.svg
2D-vektori.
Definition: Vector.cs:56
void OscillateAngle(double direction, UnlimitedAngle amplitude, double frequency, double damping=0)
Laittaa kappaleen pyörimään edestakaisin.
Definition: Movement.cs:192
Vector AbsolutePosition
Olion absoluuttinen paikka pelimaailmassa. Jos olio ei ole minkään toisen peliolion lapsiolio...
double Radians
Palauttaa tai asettaa kulman radiaaneina.
virtual Vector Position
Olion paikka. Jos olio on jonkun toisen peliolion lapsi, paikka on suhteessa tämän vanhempaan (Parent...
void StopMoveTo()
Pysäyttää MoveTo-aliohjelmalla aloitetun liikkeen.
Definition: Movement.cs:90