1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using Jypeli; |
---|
6 | |
---|
7 | |
---|
8 | abstract class Item |
---|
9 | { |
---|
10 | protected Player player; |
---|
11 | |
---|
12 | // Iso kuvake joka näkyy inventoryssä. |
---|
13 | public Image InventoryImage { get; set; } |
---|
14 | |
---|
15 | // Onko kama tällä hetkellä aktiivisessa käytössä. |
---|
16 | public virtual bool InUse { get; set; } |
---|
17 | |
---|
18 | // Jos true niin pelaaja ei koske animaatioihinsa. |
---|
19 | public virtual bool OverrideAnimation { get; set; } |
---|
20 | |
---|
21 | // Tapahtuu kokoajan, jos kama on pelaajalla valittuna tavarana. |
---|
22 | public virtual void UpdateItem(Time time) { } |
---|
23 | |
---|
24 | // Kaman käyttönappia painettiin. |
---|
25 | public virtual void UseKeyPressed() { } |
---|
26 | |
---|
27 | // Kaman käyttönappia pidetään pohjassa. |
---|
28 | public virtual void UseKeyDown() { } |
---|
29 | |
---|
30 | // Kaman käyttönappi laskettiin irti. |
---|
31 | public virtual void UseKeyReleased() { } |
---|
32 | |
---|
33 | protected Item(Player player) |
---|
34 | { |
---|
35 | this.player = player; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | class Sword : Item |
---|
40 | { |
---|
41 | public Direction SwingDirection { get; set; } |
---|
42 | |
---|
43 | public PhysicsObject SwordObject { get; set; } |
---|
44 | |
---|
45 | private readonly Angle[] swordAngles; |
---|
46 | |
---|
47 | public override bool OverrideAnimation |
---|
48 | { |
---|
49 | get { return InUse; } |
---|
50 | } |
---|
51 | |
---|
52 | public Sword(Player player) |
---|
53 | : base(player) |
---|
54 | { |
---|
55 | InventoryImage = TheLegendOfGabriel.SmallSwordImage; |
---|
56 | swordAngles = new Angle[5]; |
---|
57 | for (var i = 0; i < 5; i++) |
---|
58 | { |
---|
59 | swordAngles[i] = Angle.FromDegrees(45 + -45 * 0.5 * i); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | public override void UpdateItem(Time time) |
---|
64 | { |
---|
65 | if (SwordObject != null && SwordObject.IsDestroyed) |
---|
66 | { |
---|
67 | SwordObject = null; |
---|
68 | } |
---|
69 | if (SwordObject != null && InUse) |
---|
70 | { |
---|
71 | var dir = SwingDirection; |
---|
72 | var swordAngle = swordAngles[player.Animation.CurrentFrameIndex]; |
---|
73 | if (dir == Direction.Left) |
---|
74 | swordAngle = Angle.FromDegrees(swordAngle.Degrees * -1); |
---|
75 | SwordObject.Position = player.Position + (dir.Angle + swordAngle).GetVector() * 15; |
---|
76 | SwordObject.Angle = swordAngle + Angle.FromDegrees(dir == Direction.Left || dir == Direction.Right ? 90 : 0); |
---|
77 | SwordObject.Angle += Angle.FromDegrees(dir == Direction.Right || dir == Direction.Down ? 180 : 0); |
---|
78 | } |
---|
79 | base.UpdateItem(time); |
---|
80 | } |
---|
81 | |
---|
82 | public override void UseKeyPressed() |
---|
83 | { |
---|
84 | InUse = true; |
---|
85 | |
---|
86 | if (SwordObject != null) |
---|
87 | SwordObject.Destroy(); |
---|
88 | |
---|
89 | SwordObject = PhysicsObject.CreateStaticObject(6, 16); |
---|
90 | SwordObject.CollisionIgnoreGroup = player.CollisionIgnoreGroup; |
---|
91 | SwordObject.Position = player.Position; |
---|
92 | SwordObject.Image = TheLegendOfGabriel.SmallSwordImage; |
---|
93 | Game.Instance.Add(SwordObject); |
---|
94 | |
---|
95 | SwingDirection = player.Velocity.Angle.MainDirection; |
---|
96 | player.Animation = new Animation(player.SwingAnimations[SwingDirection]); |
---|
97 | player.Animation.StopOnLastFrame = true; |
---|
98 | player.Animation.Played += delegate |
---|
99 | { |
---|
100 | InUse = false; |
---|
101 | player.SetWalkAnimation(); |
---|
102 | SwordObject.Destroy(); |
---|
103 | }; |
---|
104 | player.Animation.Start(1); |
---|
105 | |
---|
106 | base.UseKeyPressed(); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | class Pistol : Item |
---|
111 | { |
---|
112 | private bool isCharging; |
---|
113 | |
---|
114 | public Direction ShootDirection { get; set; } |
---|
115 | |
---|
116 | // Montako sekuntia pistoolilla on tähdätty. Täydellä latauksella voisi ampua isomman ammuksen. |
---|
117 | public DoubleMeter Charge { get; set; } |
---|
118 | |
---|
119 | public override bool OverrideAnimation |
---|
120 | { |
---|
121 | get { return InUse; } |
---|
122 | } |
---|
123 | |
---|
124 | public Pistol(Player player) |
---|
125 | : base(player) |
---|
126 | { |
---|
127 | Charge = new DoubleMeter(0, 0, 4); |
---|
128 | InventoryImage = TheLegendOfGabriel.GunImage; |
---|
129 | } |
---|
130 | |
---|
131 | public override void UpdateItem(Time time) |
---|
132 | { |
---|
133 | if (isCharging) |
---|
134 | { |
---|
135 | Charge.Value += time.SinceLastUpdate.TotalSeconds; |
---|
136 | } |
---|
137 | base.UpdateItem(time); |
---|
138 | } |
---|
139 | |
---|
140 | public override void UseKeyPressed() |
---|
141 | { |
---|
142 | InUse = true; |
---|
143 | isCharging = true; // Ase alkaa latautumaan. |
---|
144 | Charge.Value = Charge.MinValue; // Aseen lataus aluksi nollaan. |
---|
145 | |
---|
146 | // Hidastetaan pelaajaa samalla kun se tähtää. |
---|
147 | player.MovementSpeed.Value = player.MovementSpeed.MaxValue * 0.333; |
---|
148 | |
---|
149 | ShootDirection = player.Velocity.Angle.MainDirection; |
---|
150 | player.Animation = new Animation(player.ShootAnimations[ShootDirection]); |
---|
151 | player.Animation.StopOnLastFrame = true; |
---|
152 | player.Animation.Start(1); |
---|
153 | base.UseKeyPressed(); |
---|
154 | } |
---|
155 | |
---|
156 | public override void UseKeyReleased() |
---|
157 | { |
---|
158 | // Pistoolilla voi ampua vain jos tähtäysanimaatio on ehtinyt loppuun. |
---|
159 | if (player.Animation.CurrentFrameIndex == player.Animation.FrameCount - 1) |
---|
160 | { |
---|
161 | var bullet = new PhysicsObject(4, 4); |
---|
162 | bullet.Position = player.Position + ShootDirection.GetVector() * player.Width * 0.7; |
---|
163 | bullet.Hit(ShootDirection.GetVector() * 500); |
---|
164 | Game.Instance.Add(bullet); |
---|
165 | } |
---|
166 | |
---|
167 | InUse = false; |
---|
168 | isCharging = false; |
---|
169 | player.MovementSpeed.Value = player.MovementSpeed.MaxValue; |
---|
170 | player.SetWalkAnimation(); |
---|
171 | |
---|
172 | base.UseKeyReleased(); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | class Monocle : Item |
---|
177 | { |
---|
178 | public Monocle(Player player) : base(player) |
---|
179 | { |
---|
180 | } |
---|
181 | |
---|
182 | public override void UpdateItem(Time time) |
---|
183 | { |
---|
184 | base.UpdateItem(time); |
---|
185 | } |
---|
186 | |
---|
187 | public override void UseKeyReleased() |
---|
188 | { |
---|
189 | InUse = true; |
---|
190 | base.UseKeyReleased(); |
---|
191 | } |
---|
192 | |
---|
193 | public override void UseKeyPressed() |
---|
194 | { |
---|
195 | InUse = false; |
---|
196 | base.UseKeyPressed(); |
---|
197 | } |
---|
198 | } |
---|