source: 2013/27/JaakkoL/RampageRebellion/RampageRebellion/RampageRebellion/RREnemy.cs @ 4461

Revision 4461, 8.2 KB checked in by jaollipa, 6 years ago (diff)
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        SoundEffect EnemyShot = Jypeli.Game.LoadSoundEffect("EnemyShot");
68        weapon.projectileGenerator.ignoresCollisionResponse = true;
69        weapon.projectileGenerator.shotSound = EnemyShot;
70        weapon.ProjectileCollision = projectileCollisionHandler;
71    }
72
73    /// <summary>
74    /// Timeout trigger. Execute whatever you need to do here
75    /// </summary>
76    public virtual void onTimerTimeout()
77    {
78        //We need to align things. So let's set the angle
79        //Let's set both for the heck of it.
80        weapon.Angle = this.Angle + Angle.FromDegrees(-90);
81        weapon.setBaseAngle(this.Angle + Angle.FromDegrees(-90));
82        PhysicsObject p = weapon.Shoot();
83
84        //So, we might need to shoot. Shall we?
85    }
86
87    /// <summary>
88    /// Creates the timer methods required for enemies shooting at their own pace
89    /// </summary>
90
91    public virtual void createShooterDelegates()
92    {
93        //Assume we need a delegate
94
95        shootingTimer = new Timer();
96        shootingTimer.Interval = shootingInterval;
97        shootingTimer.Start();
98
99        shootingTimer.Timeout += onTimerTimeout;
100    }
101
102    /// <summary>
103    /// Handles collision between an enemy and the level boundary.
104    /// </summary>
105    /// <param name="enemy">Colliding enemy</param>
106    /// <param name="target">Target of collision</param>
107    public void collisionOnBorders(RREnemy enemy, PhysicsObject target)
108    {
109        //if (!(enemy.Tag.ToString() == "B" || target.Tag.ToString() == "B")) System.Diagnostics.Debugger.Log(0, "-", "2: " + enemy.Tag.ToString() + " on " + target.Tag.ToString() + "\n");
110
111        if (onCollision != null) onCollision(enemy, target);
112
113        if (target.Tag.ToString() == "B")
114        {
115            enemy.Destroy();
116            RampageRebellion.getGame().SCOREMETER.Value -= (int)Math.Round(enemy.ScoreValue);
117        }
118    }
119
120    public void projectileCollisionHandler(PhysicsObject collider, PhysicsObject collidee)
121    {
122        if (onCollision != null) onCollision(collider, collidee);
123
124        if (!(collider.Tag.ToString() == "B" || collidee.Tag.ToString() == "B")) System.Diagnostics.Debugger.Log(0, "-", "1: " + collider.Tag.ToString() + " on " + collidee.Tag.ToString() + "\n");
125
126        RRProjectile projectile = collider as RRProjectile;
127        if (collidee.Tag.ToString() == "B" || collidee.Tag.ToString() == "BS") projectile.Destroy(); // If the bullet hits the wall or the bomb shockwave, destroy it
128        else if (collidee.Tag.ToString() == "P") //It is a player? Oh noes!
129        {
130            RRShip ship = collidee as RRShip;
131            ship.Health -= projectile.Damage;
132            if (ship.Health < 0) ship.Health = 0;
133
134            //System.Diagnostics.Debugger.Log(0, "Info", Convert.ToString(proj.ScoreValue) + "\n");
135            //RampageRebellion.getGame().SCOREMETER.Value -= rr.ScoreValue;
136            //RampageRebellion.getGame().POWERMETER.Value -= rr.PowerValue;
137            RampageRebellion.getGame().meterHandlerFunc(ship);
138
139            projectile.Destroy();
140            if (ship.Health <= 0)
141            {
142                RampageRebellion.getGame().onPlayerDeath(ship);
143            }
144        }
145    }
146}
147
148public class SmallEnemy : RREnemy
149{
150    // Base constructor requires *some* size, so give it 1.0 for now
151    public SmallEnemy(double x, double y, double multiplier = 1.0)
152        : base(1.0, 1.0, null, 1, null)
153    {
154        this.X = x;
155        this.Y = y;
156        this.Width = RampageRebellion.SMALLENEMY_SIZE;
157        this.Height = RampageRebellion.SMALLENEMY_SIZE;
158        this.Color = Color.HotPink;
159        this.Mass = 20;
160        this.LinearDamping = 0.92;
161        this.Tag = "SE";
162        this.Health = (double)RampageRebellion.SMALLENEMY_HP * multiplier;
163        this.ScoreValue = (double)RampageRebellion.SMALLENEMY_SVALUE * multiplier;
164        this.PowerValue = (double)RampageRebellion.SMALLENEMY_PVALUE * multiplier;
165        this.shootingInterval = 1.0;
166        Image smallEnemySprite = Jypeli.Game.LoadImage("SmallEnemySprite");
167        this.Image = smallEnemySprite;
168    }
169}
170
171public class MediumEnemy : RREnemy
172{
173    // Base constructor requires *some* size, so give it 1.0 for now
174    public MediumEnemy(double x, double y)
175        : base(1.0, 1.0, null, 1, null)
176    {
177        this.X = x;
178        this.Y = y;
179        this.Width = RampageRebellion.MEDENEMY_SIZE;
180        this.Height = RampageRebellion.MEDENEMY_SIZE;
181        this.Color = Color.HotPink;
182        this.Mass = 50;
183        this.LinearDamping = 0.92;
184        this.Tag = "SE";
185        this.Health = RampageRebellion.MEDENEMY_HP;
186        this.ScoreValue = RampageRebellion.MEDENEMY_SVALUE;
187        this.PowerValue = RampageRebellion.MEDENEMY_PVALUE;
188        this.shootingInterval = 1.0;
189        Image mediumEnemySprite = Jypeli.Game.LoadImage("MediumEnemySprite");
190        this.Image = mediumEnemySprite;
191    }
192}
193
194public class BigEnemy : RREnemy
195{
196    // Base constructor requires *some* size, so give it 1.0 for now
197    public BigEnemy(double x, double y)
198        : base(1.0, 1.0, null, 1, null)
199    {
200        this.X = x;
201        this.Y = y;
202        this.Width = RampageRebellion.BIGENEMY_SIZE;
203        this.Height = RampageRebellion.BIGENEMY_SIZE;
204        this.Color = Color.HotPink;
205        this.Mass = 75;
206        this.LinearDamping = 0.92;
207        this.Tag = "LE";
208        this.Health = RampageRebellion.BIGENEMY_HP;
209        this.ScoreValue = RampageRebellion.BIGENEMY_PVALUE;
210        this.shootingInterval = 1.0;
211    }
212}
213
214public class BossEnemy : RREnemy
215{
216    // Base constructor requires *some* size, so give it 1.0 for now
217    public BossEnemy(double x, double y)
218        : base(1.0, 1.0, null, 1, null)
219    {
220        this.X = x;
221        this.Y = y;
222        this.Width = RampageRebellion.BOSS_SIZE;
223        this.Height = RampageRebellion.BOSS_SIZE;
224        this.Color = Color.HotPink;
225        this.Mass = 5000;
226        this.LinearDamping = 0.92;
227        this.Tag = "LE";
228        this.Health = RampageRebellion.BOSS_HP;
229        this.ScoreValue = RampageRebellion.BOSS_SVALUE;
230        this.PowerValue = RampageRebellion.BOSS_PVALUE;
231        this.shootingInterval = 1.0;
232        Image bossEnemySprite = Jypeli.Game.LoadImage("BossSprite");
233        this.Image = bossEnemySprite;
234    }
235}
Note: See TracBrowser for help on using the repository browser.