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

Revision 4503, 8.3 KB checked in by jaollipa, 8 years ago (diff)

Fixed.

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("BlankSound");
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; // should get inherited from SmallEnemy!
97        shootingTimer.Interval = RandomGen.NextDouble(0.7, 1.5);
98        shootingTimer.Start();
99
100        shootingTimer.Timeout += onTimerTimeout;
101    }
102
103    /// <summary>
104    /// Handles collision between an enemy and the level boundary.
105    /// </summary>
106    /// <param name="enemy">Colliding enemy</param>
107    /// <param name="target">Target of collision</param>
108    public void collisionOnBorders(RREnemy enemy, PhysicsObject target)
109    {
110        //if (!(enemy.Tag.ToString() == "B" || target.Tag.ToString() == "B")) System.Diagnostics.Debugger.Log(0, "-", "2: " + enemy.Tag.ToString() + " on " + target.Tag.ToString() + "\n");
111
112        if (onCollision != null) onCollision(enemy, target);
113
114        if (target.Tag.ToString() == "B")
115        {
116            enemy.Destroy();
117            RampageRebellion.getGame().SCOREMETER.Value -= (int)Math.Round(enemy.ScoreValue);
118        }
119    }
120
121    public void projectileCollisionHandler(PhysicsObject collider, PhysicsObject collidee)
122    {
123        if (onCollision != null) onCollision(collider, collidee);
124
125        if (!(collider.Tag.ToString() == "B" || collidee.Tag.ToString() == "B")) System.Diagnostics.Debugger.Log(0, "-", "1: " + collider.Tag.ToString() + " on " + collidee.Tag.ToString() + "\n");
126
127        RRProjectile projectile = collider as RRProjectile;
128        if (collidee.Tag.ToString() == "B" || collidee.Tag.ToString() == "BS") projectile.Destroy(); // If the bullet hits the wall or the bomb shockwave, destroy it
129        else if (collidee.Tag.ToString() == "P") //It is a player? Oh noes!
130        {
131            RRShip ship = collidee as RRShip;
132            ship.Health -= projectile.Damage;
133            if (ship.Health < 0) ship.Health = 0;
134
135            //System.Diagnostics.Debugger.Log(0, "Info", Convert.ToString(proj.ScoreValue) + "\n");
136            //RampageRebellion.getGame().SCOREMETER.Value -= rr.ScoreValue;
137            //RampageRebellion.getGame().POWERMETER.Value -= rr.PowerValue;
138            RampageRebellion.getGame().meterHandlerFunc(ship);
139
140            projectile.Destroy();
141            if (ship.Health <= 0)
142            {
143                RampageRebellion.getGame().onPlayerDeath(ship);
144            }
145        }
146    }
147}
148
149public class SmallEnemy : RREnemy
150{
151    // Base constructor requires *some* size, so give it 1.0 for now
152    public SmallEnemy(double x, double y, double multiplier = 1.0)
153        : base(1.0, 1.0, null, 1, null)
154    {
155        this.X = x;
156        this.Y = y;
157        this.Width = RampageRebellion.SMALLENEMY_SIZE;
158        this.Height = RampageRebellion.SMALLENEMY_SIZE;
159        this.Color = Color.HotPink;
160        this.Mass = 20;
161        this.LinearDamping = 0.92;
162        this.Tag = "SE";
163        this.Health = (double)RampageRebellion.SMALLENEMY_HP * multiplier;
164        this.ScoreValue = (double)RampageRebellion.SMALLENEMY_SVALUE * multiplier;
165        this.PowerValue = (double)RampageRebellion.SMALLENEMY_PVALUE * multiplier;
166        this.shootingInterval = RandomGen.NextDouble(0.1, 3.0);
167        Image smallEnemySprite = Jypeli.Game.LoadImage("SmallEnemySprite");
168        this.Image = smallEnemySprite;
169    }
170}
171
172public class MediumEnemy : RREnemy
173{
174    // Base constructor requires *some* size, so give it 1.0 for now
175    public MediumEnemy(double x, double y)
176        : base(1.0, 1.0, null, 1, null)
177    {
178        this.X = x;
179        this.Y = y;
180        this.Width = RampageRebellion.MEDENEMY_SIZE;
181        this.Height = RampageRebellion.MEDENEMY_SIZE;
182        this.Color = Color.HotPink;
183        this.Mass = 50;
184        this.LinearDamping = 0.92;
185        this.Tag = "SE";
186        this.Health = RampageRebellion.MEDENEMY_HP;
187        this.ScoreValue = RampageRebellion.MEDENEMY_SVALUE;
188        this.PowerValue = RampageRebellion.MEDENEMY_PVALUE;
189        this.shootingInterval = 1.0;
190        Image mediumEnemySprite = Jypeli.Game.LoadImage("MediumEnemySprite");
191        this.Image = mediumEnemySprite;
192    }
193}
194
195public class BigEnemy : RREnemy
196{
197    // Base constructor requires *some* size, so give it 1.0 for now
198    public BigEnemy(double x, double y)
199        : base(1.0, 1.0, null, 1, null)
200    {
201        this.X = x;
202        this.Y = y;
203        this.Width = RampageRebellion.BIGENEMY_SIZE;
204        this.Height = RampageRebellion.BIGENEMY_SIZE;
205        this.Color = Color.HotPink;
206        this.Mass = 75;
207        this.LinearDamping = 0.92;
208        this.Tag = "LE";
209        this.Health = RampageRebellion.BIGENEMY_HP;
210        this.ScoreValue = RampageRebellion.BIGENEMY_PVALUE;
211        this.shootingInterval = 1.0;
212    }
213}
214
215public class BossEnemy : RREnemy
216{
217    // Base constructor requires *some* size, so give it 1.0 for now
218    public BossEnemy(double x, double y)
219        : base(1.0, 1.0, null, 1, null)
220    {
221        this.X = x;
222        this.Y = y;
223        this.Width = RampageRebellion.BOSS_SIZE;
224        this.Height = RampageRebellion.BOSS_SIZE;
225        this.Color = Color.HotPink;
226        this.Mass = 5000;
227        this.LinearDamping = 0.92;
228        this.Tag = "LE";
229        this.Health = RampageRebellion.BOSS_HP;
230        this.ScoreValue = RampageRebellion.BOSS_SVALUE;
231        this.PowerValue = RampageRebellion.BOSS_PVALUE;
232        this.shootingInterval = 1.0;
233        Image bossEnemySprite = Jypeli.Game.LoadImage("BossSprite");
234        this.Image = bossEnemySprite;
235    }
236}
Note: See TracBrowser for help on using the repository browser.