Jypeli  9
The simple game programming library
Tank.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: Vesa Lappalainen, Tero Jäntti, Tomi Karppinen, Janne Nikkanen.
28  */
29 
30 using System;
31 using System.Collections.Generic;
32 
33 namespace Jypeli.Assets
34 {
38  public class Tank : PhysicsObject
39  {
40  private static Image commonImage = null;
41  private static Shape commonShape = null;
42 
43  private Cannon cannon;
44  private List<PhysicsObject> wheels = new List<PhysicsObject>();
45  private List<IAxleJoint> joints = new List<IAxleJoint>();
46  //private IntMeter ammo = new IntMeter( 10 );
47  private IntMeter hitPoints = new IntMeter( 10 );
48 
52  public override Vector Size
53  {
54  get { return base.Size; }
55  set { throw new NotImplementedException( "The size of the tank can not be changed." ); }
56  }
57 
63  {
64  get { return hitPoints; }
65  }
66 
70  public IntMeter Ammo { get { return Cannon.Ammo; } }
71 
75  public Cannon Cannon { get { return cannon; } }
76 
77 
81  public Tank( double width, double height )
82  : base( width, height )
83  {
84  if ( commonImage == null )
85  commonImage = Game.LoadImageFromResources( "Tank.png" );
86  if ( commonShape == null )
90  HitPoints.LowerLimit += Break;
92 
93  cannon = new Cannon( Width * 0.75, Height * 0.2 );
94  Cannon.Position = new Vector(0, Height * 0.25);
95  Cannon.TimeBetweenUse = TimeSpan.FromSeconds( 0.5 );
96  Cannon.Ammo.Value = 100;
97  this.Add( Cannon );
98 
99  AddedToGame += AddWheels;
100  }
101 
102  private void AddWheels()
103  {
105  if ( pg == null ) throw new InvalidOperationException( "Cannot have a tank in non-physics game" );
106 
107  const int wheelCount = 6;
108 
109  double r = this.Width / ( 2 * wheelCount );
110  double left = this.X - this.Width / 2 + r;
111  double[] wheelYPositions = new double[wheelCount];
112  for ( int i = 0; i < wheelYPositions.Length; i++ )
113  wheelYPositions[i] = this.Y - this.Height / 2;
114  wheelYPositions[0] = wheelYPositions[wheelCount - 1] = this.Position.Y - ( this.Height * 3 / 8 );
115 
116  for ( int i = 0; i < wheelCount; i++ )
117  {
118  PhysicsObject wheel = new PhysicsObject( 2 * r, 2 * r, Shape.Circle );
119  wheel.Mass = this.Mass / 20;
120  wheel.Color = Color.Gray;
121  wheel.CollisionIgnorer = this.CollisionIgnorer;
122  wheel.Body.AngularDamping = 0.95f;
123  wheel.KineticFriction = 1.0;
124  wheels.Add( wheel );
125  pg.Add( wheel );
126 
127  Vector axlePos = new Vector( left + i * ( this.Width / wheelCount ), wheelYPositions[i] );
128  wheel.Position = axlePos;
129  IAxleJoint joint = pg.Engine.CreateJoint( this, wheel, new Vector( axlePos.X, axlePos.Y ) );
130  joint.Softness = 0.01f;
131  joints.Add( joint );
132  pg.Add( joint );
133  }
134  }
135 
136  public override void Destroy()
137  {
138  foreach ( var j in joints )
139  j.Destroy();
140  foreach ( var w in wheels )
141  w.Destroy();
142  Cannon.Destroy();
143  base.Destroy();
144  }
145 
146  private void Break()
147  {
148  Explosion rajahdys = new Explosion( 150 );
149  rajahdys.Force = 10;
150  rajahdys.Position = Position;
151  Game.Instance.Add( rajahdys );
152 
153  this.Destroy();
154  }
155 
160  public void Accelerate( double power )
161  {
162  double realPower = power;
163  if ( power > 1.0 )
164  realPower = 1.0;
165  else if ( power < -1.0 )
166  realPower = -1.0;
167 
168  double torque = Mass * realPower * 3000;
169 
170  foreach ( var w in wheels )
171  {
172  w.Body.ApplyTorque( (float)(torque / wheels.Count) );
173  }
174  }
175 
180  public void Shoot( double power )
181  {
182  Cannon.Power.Value = power;
183  Shoot();
184  }
185 
189  public void Shoot()
190  {
191  Cannon.Shoot();
192  }
193  }
194 }
Jypeli.Assets.Tank.hitPoints
IntMeter hitPoints
Definition: Tank.cs:47
Jypeli.Assets.Tank.Shoot
void Shoot(double power)
Ampuu halutulla voimalla.
Definition: Tank.cs:180
Jypeli.Assets.Tank.Break
void Break()
Definition: Tank.cs:146
Jypeli.PhysicsObject.Body
IPhysicsBody Body
Definition: Dimensions.cs:10
Jypeli.Assets.Tank.Tank
Tank(double width, double height)
Alustaa uuden tankin.
Definition: Tank.cs:81
Jypeli.Vector.X
double X
Definition: Vector.cs:312
Jypeli.GameObject.Destroy
override void Destroy()
Tuhoaa olion. Tuhottu olio poistuu pelistä.
Definition: GameObject.cs:52
Jypeli.Assets.Tank
Yksinkertainen tankki eli panssarivaunu.
Definition: Tank.cs:39
Jypeli.PhysicsGameBase
Kantaluokka fysiikkapeleille.
Definition: PhysicsGameBase.cs:12
Jypeli.Assets.Explosion
Räjähdys.
Definition: Explosion.cs:38
Jypeli.Shape
Kuvio.
Definition: Shapes.cs:47
Jypeli.PhysicsGameBase.Add
void Add(IAxleJoint j)
Lisää liitoksen peliin.
Definition: PhysicsGameBase.cs:189
Jypeli.Assets.Tank.HitPoints
IntMeter HitPoints
Tankin osumapisteet. Kun nämä menevät nollaan, tankki hajoaa.
Definition: Tank.cs:63
Jypeli.Assets.Weapon.Power
DoubleMeter Power
Voima, jolla panos ammutaan. Nollautuu ammuttaessa.
Definition: Weapon.cs:61
Jypeli.Assets.Tank.Cannon
Cannon Cannon
Tankin piippu.
Definition: Tank.cs:75
Jypeli.Assets.Cannon
Yksinkertainen tykki, joka ampuu kuulia tai muuta ammuksia.
Definition: WeaponTemplates.cs:40
Jypeli.Game.Instance
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:90
Jypeli.PhysicsObject.KineticFriction
double KineticFriction
Liikekitka (hidastaa kun olio on jo liikkeessä). Ks. StaticFriction (lepokitka)
Definition: Collisions.cs:88
Jypeli.Assets.Tank.Size
override Vector Size
Tankin koko. Tätä ei voi muuttaa.
Definition: Tank.cs:53
Jypeli.Assets.Tank.Ammo
IntMeter Ammo
Ammusten määrä.
Definition: Tank.cs:70
Jypeli.Assets.Weapon.Shoot
PhysicsObject Shoot()
Ampuu aseella, ja palauttaa ammuksen tai null, jos ampuminen ei onnistu (esimerkiksi jos panokset ova...
Definition: Weapon.cs:174
Jypeli.Game.Add
void Add(Light light)
Lisää valon peliin. Nykyisellään valoja voi olla ainoastaan yksi kappale. Toistaiseksi ei tuettu Wind...
Definition: Effects.cs:27
Jypeli.PhysicsObject.CollisionIgnorer
virtual Ignorer CollisionIgnorer
Olio, jolla voi välttää oliota osumasta tiettyihin muihin olioihin.
Definition: Collisions.cs:13
Jypeli.Assets.Tank.Destroy
override void Destroy()
Definition: Tank.cs:136
Jypeli.Game.LoadImageFromResources
static Image LoadImageFromResources(string name)
Lataa kuvan Jypelin sisäisistä resursseista.
Definition: Content.cs:84
Jypeli.PhysicsObject.PhysicsObject
PhysicsObject(double width, double height, Shape shape, double x=0.0, double y=0.0)
Alustaa fysiikkaolion käyttöön.
Definition: PhysicsObject.cs:44
Jypeli.Assets.Tank.AddWheels
void AddWheels()
Definition: Tank.cs:102
Jypeli.Assets.Explosion.Force
double Force
Voima, jolla räjähdyksen paineaallon uloin reuna heittää olioita räjähdyksestä poispäin....
Definition: Explosion.cs:94
Jypeli.Shape.Circle
static readonly Ellipse Circle
Ympyrä tai ellipsi.
Definition: Shapes.cs:60
Jypeli.IAxleJoint.Softness
double Softness
Liitoksen pehmeys eli kuinka paljon sillä on liikkumavaraa.
Definition: IAxleJoint.cs:55
Jypeli.Color
Väri.
Definition: Color.cs:13
Jypeli.PhysicsObject.Position
override Vector Position
Definition: Dimensions.cs:27
Jypeli.IAxleJoint
Definition: IAxleJoint.cs:36
Jypeli.Shape.FromImage
static Shape FromImage(Image image)
Luo kuvion annetusta kuvasta. Kuvassa tulee olla vain yksi yhtenäinen muoto (toisin sanoen kuvio ei v...
Definition: Shapes.cs:116
Jypeli.Assets.Tank.Accelerate
void Accelerate(double power)
Kiihdyttää tankkia.
Definition: Tank.cs:160
Jypeli.Assets.Tank.wheels
List< PhysicsObject > wheels
Definition: Tank.cs:44
Jypeli.Assets.Weapon.Ammo
IntMeter Ammo
Panosten määrä.
Definition: Weapon.cs:66
Jypeli.Image
Kuva.
Definition: Image.cs:29
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
System
Definition: CFFauxAttributes.cs:29
Jypeli.PhysicsGameBase.Engine
IPhysicsEngine Engine
Definition: PhysicsGameBase.cs:29
Jypeli.IntMeter
Mittari, joka mittaa int-tyyppisiä arvoja. Sidottavissa näyttöihin, kuten ValueDisplay ja BarGauge...
Definition: IntMeter.cs:11
Jypeli.PhysicsObject
Definition: Collisions.cs:6
Jypeli.Assets
Definition: Automobile.cs:5
Jypeli.Assets.Tank.joints
List< IAxleJoint > joints
Definition: Tank.cs:45
Jypeli.Assets.Tank.Shoot
void Shoot()
Ampuu tankin tykillä, jos ammuksia on vielä jäljellä.
Definition: Tank.cs:189
Jypeli.ObjectIgnorer
A collision ignorer that uses reference comparison. All Bodies with the same instance of this ignorer...
Definition: ObjectIgnorer.cs:41
Jypeli.Color.Gray
static readonly Color Gray
Harmaa.
Definition: Color.cs:643
Jypeli.Assets.Tank.commonImage
static Image commonImage
Definition: Tank.cs:40
Jypeli.PhysicsObject.Mass
double Mass
Olion massa (paino). Mitä enemmän massaa, sitä enemmän voimaa tarvitaan saamaan olio liikkeelle / pys...
Definition: Inertia.cs:14
Jypeli.Game
Definition: Content.cs:46
Jypeli.Assets.Weapon.TimeBetweenUse
TimeSpan TimeBetweenUse
Kuinka kauan kestää, että asetta voidaan käyttää uudestaan.
Definition: Weapon.cs:107
Jypeli.Assets.Tank.commonShape
static Shape commonShape
Definition: Tank.cs:41
Jypeli.Assets.Tank.cannon
Cannon cannon
Definition: Tank.cs:43
Jypeli.Vector.Y
double Y
Definition: Vector.cs:313