-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActivatedBonus.cs
147 lines (131 loc) · 4.78 KB
/
ActivatedBonus.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TexturePackerLoader;
namespace Match3
{
class ActivatedBonusBase : DrawableGameComponent
{
public float scale = 1;
public bool ended = false;
public ActivatedBonusBase(Game game) : base(game) { }
}
class LineBonus : ActivatedBonusBase
{
float shift;
Vector2 start;
Vector2 position;
Vector2 end;
float duration = 1;
float elapsed = 0;
AnimatedTexture texture;
public delegate void LineBonusEventHandler(LineBonus lineBonus, LineBonusEventAgrs e);
public event LineBonusEventHandler onEnd;
public event LineBonusEventHandler onUpdate;
public class LineBonusEventAgrs : EventArgs
{
public Vector2 prevPosition;
public Vector2 newPosition;
public LineBonusEventAgrs(Vector2 prevPosition, Vector2 newPosition)
{
this.prevPosition = prevPosition;
this.newPosition = newPosition;
}
}
public LineBonus(Game game, Vector2 start, Vector2 end, Vector2 dir, float shift)
: base(game)
{
this.texture = new GlowingBallTexture(game.GraphicsDevice, 16, 16);
this.shift = shift;
this.position = start;
this.end = end + dir * Config.tile.Size.ToVector2() / 2;
this.start = start;
Vector2 d = (this.start - this.end) / shift;
this.duration = Math.Abs(d.X - d.Y);
this.position = start;
}
public override void Update(GameTime gameTime)
{
float elapsedGameTime = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (!ended) {
elapsed += elapsedGameTime;
Vector2 prevPos = position;
position = start + (end - start) * elapsed / duration;
texture.Update(gameTime);
if (onUpdate != null)
onUpdate(this, new LineBonusEventAgrs(prevPos, position));
if (duration < elapsed) {
ended = true;
position = end;
if (onEnd != null)
onEnd(this, new LineBonusEventAgrs(prevPos, position));
}
}
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
if (!ended) {
SpriteBatch sb = Game.Services.GetService<SpriteBatch>();
SpriteRender sr = Game.Services.GetService<SpriteRender>();
sb.Begin();
texture.Draw(gameTime, sr, position, scale);
sb.End();
base.Draw(gameTime);
}
}
}
class BombBonus : ActivatedBonusBase
{
public Element el { get; private set; }
AnimatedTexture explosionTexture;
double elapsed = 0;
bool exploded = false;
public delegate void BombBonusEventHandler(BombBonus bombBonus);
public event BombBonusEventHandler onExplosion;
public BombBonus(Game game, Element el)
: base(game)
{
this.explosionTexture = new ExplosionTexture(GraphicsDevice, Config.tile.Width * 3, Config.tile.Height * 3);
this.el = el;
this.scale = 5;
}
public override void Update(GameTime gameTime)
{
elapsed += gameTime.ElapsedGameTime.TotalMilliseconds;
if (elapsed < Config.bombBonusDuration) {
float v = 255 - (float)(255 * elapsed / Config.bombBonusDuration);
el.color = new Color(255, v, v);
} else if (!exploded) {
exploded = true;
el.color = Color.Red;
if (onExplosion != null)
onExplosion(this);
}
if (exploded)
explosionTexture.Update(gameTime);
if (explosionTexture.ended) ended = true;
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
if (!ended) {
if (!exploded) {
if (el.dying)
el.Draw(gameTime);
} else {
SpriteBatch sb = Game.Services.GetService<SpriteBatch>();
SpriteRender sr = Game.Services.GetService<SpriteRender>();
sb.Begin();
explosionTexture.Draw(gameTime, sr, el.rectangle.Center.ToVector2(), scale);
sb.End();
}
}
base.Draw(gameTime);
}
}
}