source: 2013/27/ArttuY/trunk/RampageRebellion/RampageRebellion/RampageRebellion/RREnemy.cs @ 10337

Revision 4451, 7.9 KB checked in by eearyla, 7 years ago (diff)

Backup

Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Jypeli;
6using Jypeli.Assets;
7using Jypeli.Controls;
8using Jypeli.Effects;
9using Jypeli.Widgets;
10
11/// <summary>
12/// Base class for enemies.
13/// </summary>
14public class RREnemy : RRObject
15{
16    public double shootingInterval = 0;
17    private PhysicsGame thisGame = null;
18    private Action<PhysicsObject, PhysicsObject> onCollision;
19
20    private Timer shootingTimer;
21
22    public RRWeapon weapon;
23
24    public RREnemy(double width, double height)
25        : base(width, height)
26    {
27        this.Tag = "E";
28        this.CollisionIgnoreGroup = 4;
29        this.IgnoresExplosions = true;
30        this.CanRotate = false;
31        //this.IgnoresGravity = true;
32    }
33
34    public override void Destroy()
35    {
36        if (shootingTimer != null) { shootingTimer.Stop(); shootingTimer = null; }
37
38        Explosion expl = new Explosion(100);
39        expl.Position = this.Position;
40        RampageRebellion.getGame().Add(expl);
41
42        base.Destroy();
43    }
44
45    /// <summary>
46    /// Creates a new RREnemy
47    /// </summary>
48    /// <param name="x">XPosition</param>
49    /// <param name="y">YPosition</param>
50    /// <param name="gameObject">Game object</param>
51    /// <param name="shootingInterval">How often this object shall shoot?</param>
52    /// <param name="onColl">Collision handler</param>
53    public RREnemy(double x, double y, PhysicsGame gameObject, double shootingInterval, Action<PhysicsObject, PhysicsObject> onColl)
54        : this(x, y)
55    {
56        thisGame = gameObject;
57        this.shootingInterval = shootingInterval;
58        this.onCollision = onColl;
59
60        weapon = new RRWeapon(20, 20, true, 50.0, 10.0);
61        weapon.randomizedAngleSlate = 10;
62        this.Add(weapon);
63
64        createShooterDelegates();
65
66        RampageRebellion.getGame().AddCollisionHandler<RREnemy, PhysicsObject>(this, collisionOnBorders);
67        weapon.ProjectileCollision = projectileCollisionHandler;
68    }
69
70    /// <summary>
71    /// Timeout trigger. Execute whatever you need to do here
72    /// </summary>
73    public virtual void onTimerTimeout()
74    {
75        //We need to align things. So let's set the angle
76        //Let's set both for the heck of it.
77        weapon.Angle = this.Angle + Angle.FromDegrees(-90);
78        weapon.setBaseAngle(this.Angle + Angle.FromDegrees(-90));
79        PhysicsObject p = weapon.Shoot();
80
81        //So, we might need to shoot. Shall we?
82    }
83
84    /// <summary>
85    /// Creates the timer methods required for enemies shooting at their own pace
86    /// </summary>
87
88    public virtual void createShooterDelegates()
89    {
90        //Assume we need a delegate
91
92        shootingTimer = new Timer();
93        shootingTimer.Interval = shootingInterval;
94        shootingTimer.Start();
95
96        shootingTimer.Timeout += onTimerTimeout;
97    }
98
99    /// <summary>
100    /// Handles collision between an enemy and the level boundary.
101    /// </summary>
102    /// <param name="enemy">Colliding enemy</param>
103    /// <param name="target">Target of collision</param>
104    public void collisionOnBorders(RREnemy enemy, PhysicsObject target)
105    {
106        //if (!(enemy.Tag.ToString() == "B" || target.Tag.ToString() == "B")) System.Diagnostics.Debugger.Log(0, "-", "2: " + enemy.Tag.ToString() + " on " + target.Tag.ToString() + "\n");
107
108        if (onCollision != null) onCollision(enemy, target);
109
110        if (target.Tag.ToString() == "B")
111        {
112            enemy.Destroy();
113            RampageRebellion.getGame().SCOREMETER.Value -= (int)Math.Round(enemy.ScoreValue);
114        }
115    }
116
117    public void projectileCollisionHandler(PhysicsObject collider, PhysicsObject collidee)
118    {
119        if (onCollision != null) onCollision(collider, collidee);
120
121        if (!(collider.Tag.ToString() == "B" || collidee.Tag.ToString() == "B")) System.Diagnostics.Debugger.Log(0, "-", "1: " + collider.Tag.ToString() + " on " + collidee.Tag.ToString() + "\n");
122
123        RRProjectile projectile = collider as RRProjectile;
124        if (collidee.Tag.ToString() == "B" || collidee.Tag.ToString().EndsWith("E")) projectile.Destroy(); // If the bullet hits the wall or an enemy, destroy it
125        else if (collidee.Tag.ToString() == "P") //It is a player? Oh noes!
126        {
127            RRShip ship = collidee as RRShip;
128            ship.Health -= projectile.Damage;
129            if (ship.Health < 0) ship.Health = 0;
130
131            //System.Diagnostics.Debugger.Log(0, "Info", Convert.ToString(proj.ScoreValue) + "\n");
132            //RampageRebellion.getGame().SCOREMETER.Value -= rr.ScoreValue;
133            //RampageRebellion.getGame().POWERMETER.Value -= rr.PowerValue;
134            RampageRebellion.getGame().meterHandlerFunc(ship);
135
136            projectile.Destroy();
137            if (ship.Health <= 0)
138            {
139                RampageRebellion.getGame().onPlayerDeath(ship);
140            }
141        }
142    }
143}
144
145public class SmallEnemy : RREnemy
146{
147    // Base constructor requires *some* size, so give it 1.0 for now
148    public SmallEnemy(double x, double y, double multiplier = 1.0)
149        : base(1.0, 1.0, null, 1, null)
150    {
151        this.X = x;
152        this.Y = y;
153        this.Width = RampageRebellion.SMALLENEMY_SIZE;
154        this.Height = RampageRebellion.SMALLENEMY_SIZE;
155        this.Color = Color.HotPink;
156        this.Mass = 20;
157        this.LinearDamping = 0.92;
158        this.Tag = "SE";
159        this.Health = (double)RampageRebellion.SMALLENEMY_HP * multiplier;
160        this.ScoreValue = (double)RampageRebellion.SMALLENEMY_SVALUE * multiplier;
161        this.PowerValue = (double)RampageRebellion.SMALLENEMY_PVALUE * multiplier;
162        this.shootingInterval = 1.0;
163        Image smallEnemySprite = Jypeli.Game.LoadImage("SmallEnemySprite");
164        this.Image = smallEnemySprite;
165    }
166}
167
168public class MediumEnemy : RREnemy
169{
170    // Base constructor requires *some* size, so give it 1.0 for now
171    public MediumEnemy(double x, double y)
172        : base(1.0, 1.0, null, 1, null)
173    {
174        this.X = x;
175        this.Y = y;
176        this.Width = RampageRebellion.MEDENEMY_SIZE;
177        this.Height = RampageRebellion.MEDENEMY_SIZE;
178        this.Color = Color.HotPink;
179        this.Mass = 50;
180        this.LinearDamping = 0.92;
181        this.Tag = "SE";
182        this.Health = RampageRebellion.MEDENEMY_HP;
183        this.ScoreValue = RampageRebellion.MEDENEMY_SVALUE;
184        this.PowerValue = RampageRebellion.MEDENEMY_PVALUE;
185        this.shootingInterval = 1.0;
186        Image mediumEnemySprite = Jypeli.Game.LoadImage("MediumEnemySprite");
187        this.Image = mediumEnemySprite;
188    }
189}
190
191public class BigEnemy : RREnemy
192{
193    // Base constructor requires *some* size, so give it 1.0 for now
194    public BigEnemy(double x, double y)
195        : base(1.0, 1.0, null, 1, null)
196    {
197        this.X = x;
198        this.Y = y;
199        this.Width = RampageRebellion.BIGENEMY_SIZE;
200        this.Height = RampageRebellion.BIGENEMY_SIZE;
201        this.Color = Color.HotPink;
202        this.Mass = 75;
203        this.LinearDamping = 0.92;
204        this.Tag = "LE";
205        this.Health = RampageRebellion.BIGENEMY_HP;
206        this.ScoreValue = RampageRebellion.BIGENEMY_PVALUE;
207        this.shootingInterval = 1.0;
208    }
209}
210
211public class BossEnemy : RREnemy
212{
213    // Base constructor requires *some* size, so give it 1.0 for now
214    public BossEnemy(double x, double y)
215        : base(1.0, 1.0, null, 1, null)
216    {
217        this.X = x;
218        this.Y = y;
219        this.Width = RampageRebellion.BOSS_SIZE;
220        this.Height = RampageRebellion.BOSS_SIZE;
221        this.Color = Color.HotPink;
222        this.Mass = 5000;
223        this.LinearDamping = 0.92;
224        this.Tag = "LE";
225        this.Health = RampageRebellion.BOSS_HP;
226        this.ScoreValue = RampageRebellion.BOSS_SVALUE;
227        this.PowerValue = RampageRebellion.BOSS_PVALUE;
228        this.shootingInterval = 1.0;
229    }
230}
Note: See TracBrowser for help on using the repository browser.