source: 2015/27/ohjaajat/TheLegendOfGabriel/TheLegendOfGabriel/TheLegendOfGabriel/Item.cs @ 6631

Revision 6631, 8.5 KB checked in by sieerinn, 8 years ago (diff)

Miekan ääntä säädetty hiljaisemmaksi. Kranaatteja voi heitellä.

Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Jypeli;
6using Jypeli.Assets;
7
8
9abstract class Item
10{
11    protected Player player;
12
13    // Iso kuvake joka näkyy inventoryssä.
14    public Image InventoryImage { get; set; }
15
16    public IntMeter Usages { get; set; }
17
18    // Onko kama tällä hetkellä aktiivisessa käytössä.
19    public virtual bool InUse { get; set; }
20
21    // Jos true niin pelaaja ei koske animaatioihinsa.
22    public virtual bool OverrideAnimation { get; set; }
23
24    // Tapahtuu kokoajan, jos kama on pelaajalla valittuna tavarana.
25    public virtual void UpdateItem(Time time) { }
26
27    // Kaman käyttönappia painettiin.
28    public virtual void UseKeyPressed() { }
29
30    // Kaman käyttönappia pidetään pohjassa.
31    public virtual void UseKeyDown() { }
32
33    // Kaman käyttönappi laskettiin irti.
34    public virtual void UseKeyReleased() { }
35
36    public virtual void CancelUse() { }
37
38    protected Item(Player player)
39    {
40        Usages = new IntMeter(0, 0, 0);
41        this.player = player;
42    }
43}
44
45class Sword : Item
46{
47    public Direction SwingDirection { get; set; }
48
49    public PhysicsObject SwordObject { get; set; }
50
51    private readonly Angle[] swordAngles;
52
53    public override bool OverrideAnimation
54    {
55        get { return InUse; }
56    }
57
58    public Sword(Player player)
59        : base(player)
60    {
61        InventoryImage = TheLegendOfGabriel.SmallSwordImage;
62        swordAngles = new Angle[5];
63        for (var i = 0; i < 5; i++)
64        {
65            swordAngles[i] = Angle.FromDegrees(45 + -45 * 0.5 * i);
66        }
67    }
68
69    public override void UpdateItem(Time time)
70    {
71        if (SwordObject != null && SwordObject.IsDestroyed)
72        {
73            SwordObject = null;
74        }
75        if (SwordObject != null && InUse)
76        {
77            if (player.Animation.CurrentFrameIndex < swordAngles.Length)
78            {
79                try
80                {
81                    var dir = SwingDirection;
82                    var swordAngle = swordAngles[player.Animation.CurrentFrameIndex];
83                    if (dir == Direction.Left)
84                        swordAngle = Angle.FromDegrees(swordAngle.Degrees*-1);
85                    SwordObject.Position = player.Position + (dir.Angle + swordAngle).GetVector()*15;
86                    SwordObject.Angle = swordAngle +
87                                        Angle.FromDegrees(dir == Direction.Left || dir == Direction.Right ? 90 : 0);
88                    SwordObject.Angle += Angle.FromDegrees(dir == Direction.Right || dir == Direction.Down ? 180 : 0);
89                }
90                catch (IndexOutOfRangeException ex)
91                {
92                   
93                }
94            }
95        }
96        base.UpdateItem(time);
97    }
98
99    public override void UseKeyPressed()
100    {
101        InUse = true;
102
103        TheLegendOfGabriel.SwordSound.Play();
104
105        if (SwordObject != null)
106            SwordObject.Destroy();
107
108        SwordObject = PhysicsObject.CreateStaticObject(6, 16);
109        SwordObject.CollisionIgnoreGroup = player.CollisionIgnoreGroup;
110        SwordObject.Position = player.Position;
111        SwordObject.Image = TheLegendOfGabriel.SmallSwordImage;
112        Game.Instance.Add(SwordObject);
113
114        SwingDirection = player.Velocity.Angle.MainDirection;
115        player.Animation = new Animation(player.SwingAnimations[SwingDirection]);
116        player.Animation.StopOnLastFrame = true;
117        player.Animation.Played += delegate
118        {
119            InUse = false;
120            player.SetWalkAnimation();
121            SwordObject.Destroy();
122        };
123        player.Animation.Start(1);
124
125        base.UseKeyPressed();
126    }
127
128    public override void CancelUse()
129    {
130        InUse = false;
131        player.SetWalkAnimation();
132        if (SwordObject != null)
133        {
134            SwordObject.Destroy();
135            SwordObject = null;
136        }
137    }
138}
139
140class Pistol : Item
141{
142    private bool isCharging;
143
144    public Direction ShootDirection { get; set; }
145
146    // Montako sekuntia pistoolilla on tähdätty. Täydellä latauksella voisi ampua isomman ammuksen.
147    public DoubleMeter Charge { get; set; }
148
149    public override bool OverrideAnimation
150    {
151        get { return InUse; }
152    }
153
154    public Pistol(Player player)
155        : base(player)
156    {
157        Usages.MaxValue = 15;
158        Usages.Value = 15;
159        Charge = new DoubleMeter(0, 0, 4);
160        InventoryImage = TheLegendOfGabriel.GunImage;
161    }
162
163    public override void UpdateItem(Time time)
164    {
165        if (isCharging)
166        {
167            Charge.Value += time.SinceLastUpdate.TotalSeconds;
168        }
169        base.UpdateItem(time);
170    }
171
172    public override void UseKeyPressed()
173    {
174        if (Usages.Value == 0)
175        {
176            base.UseKeyPressed();
177            return;
178        }
179
180        InUse = true;
181        isCharging = true; // Ase alkaa latautumaan.
182        Charge.Value = Charge.MinValue; // Aseen lataus aluksi nollaan.
183
184        // Hidastetaan pelaajaa samalla kun se tähtää.
185        player.MovementSpeed.Value = player.MovementSpeed.MaxValue * 0.333;
186
187        ShootDirection = player.Velocity.Angle.MainDirection;
188        player.Animation = new Animation(player.ShootAnimations[ShootDirection]);
189        player.Animation.StopOnLastFrame = true;
190        player.Animation.Start(1);
191        base.UseKeyPressed();
192    }
193
194    public override void UseKeyReleased()
195    {
196        // Pistoolilla voi ampua vain jos tähtäysanimaatio on ehtinyt loppuun.
197        if (InUse && Usages.Value > 0 && player.Animation.CurrentFrameIndex == player.Animation.FrameCount - 1)
198        {
199            Usages.Value--;
200            TheLegendOfGabriel.GunSound.Play();
201
202            var bullet = new PhysicsObject(4, 4);
203            bullet.Image = TheLegendOfGabriel.BulletImage;
204            bullet.Position = player.Position + ShootDirection.GetVector() * player.Width * 0.7;
205            bullet.Hit(ShootDirection.GetVector() * 500);
206            Game.Instance.Add(bullet);
207        }
208        CancelUse();
209
210        base.UseKeyReleased();
211    }
212
213    public override void CancelUse()
214    {
215        InUse = false;
216        isCharging = false;
217        player.MovementSpeed.Value = player.MovementSpeed.MaxValue;
218        player.SetWalkAnimation();
219        base.CancelUse();
220    }
221}
222
223class Grenade : Item
224{
225    public Grenade(Player player) : base(player)
226    {
227        Usages.MaxValue = 15;
228        Usages.Value = 15;
229        InventoryImage = TheLegendOfGabriel.GrenadeImage;
230    }
231
232    public override void UseKeyPressed()
233    {
234        if (Usages.Value > 0)
235        {
236            Usages.Value--;
237
238            var grenade = new PhysicsObject(6, 6);
239            grenade.Image = TheLegendOfGabriel.GrenadeSmallImage;
240            grenade.Position = player.Position + player.LastDirection.GetVector() * player.Width * 0.7;
241            grenade.Hit(player.LastDirection.GetVector() * 500);
242            grenade.ApplyTorque(9000);
243            grenade.LinearDamping = 0.9;
244            grenade.AngularDamping = 0.9;
245            Game.Instance.Add(grenade);
246
247            Timer.SingleShot(1.5, delegate
248            {
249                var expl = new Explosion(50);
250                expl.Position = grenade.Position;
251                Game.Instance.Add(expl);
252                grenade.Destroy();
253            });
254        }
255        base.UseKeyPressed();
256    }
257}
258
259class Monocle : Item
260{
261    private GameObject bigMonocle;
262
263    public Monocle(Player player) : base(player)
264    {
265        InventoryImage = TheLegendOfGabriel.MonocleImage;
266    }
267
268    public override void UpdateItem(Time time)
269    {
270        if (bigMonocle != null)
271            bigMonocle.Position = player.Position;
272        base.UpdateItem(time);
273    }
274
275    public override void UseKeyPressed()
276    {
277        InUse = true;
278        CreateMonocle();
279        base.UseKeyPressed();
280    }
281
282    public override void UseKeyReleased()
283    {
284        CancelUse();
285        base.UseKeyReleased();
286    }
287
288    public override void CancelUse()
289    {
290        InUse = false;
291        if (bigMonocle != null)
292        {
293            bigMonocle.Destroy();
294            bigMonocle = null;
295        }
296        base.CancelUse();
297    }
298
299    private void CreateMonocle()
300    {
301        bigMonocle = new GameObject(150, 150);
302        bigMonocle.Image = TheLegendOfGabriel.BigMonocleImage;
303        bigMonocle.Position = player.Position;
304        Game.Instance.Add(bigMonocle, 3);
305    }
306}
Note: See TracBrowser for help on using the repository browser.