Maze /Classes/Object/Slug.cs

Language C# Lines 307
MD5 Hash 59f7b6acdcef94dd955d2410a642fa22
Repository git://github.com/Concliff/Maze.git View Raw File View Project SPDX
  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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Maze.Classes
{
    /// <summary>
    /// Represents the unit that is controlled by a player.
    /// </summary>
    public class Slug : Unit
    {
        /// <summary>
        /// Max value of energy the Slug may have.
        /// </summary>
        public const int MaxOozeEnergy = 100;

        private int downTime; // stand still time
        private int travelTime; // in motion time
        private bool isInMotion;

        /// <summary>
        /// Total game score of this Slug
        /// </summary>
        private int score;

        /// <summary>
        /// Count of Drops that Player has collected at current level.
        /// </summary>
        private int collectedDropsCount;

        /// <summary>
        /// Initialized a new instance of the Slug class.
        /// </summary>
        public Slug()
        {
            Name = "Noname";
            ObjectType = ObjectTypes.Slug;
            this.unitType = UnitTypes.Slug;
            this.unitSide = UnitSides.Good;

            // Home Poisition will be initialized after the Level loading.

            this.respawnTimer = 3000;

            OozeEnergy = MaxOozeEnergy;

            objectSize.Width = GlobalConstants.PLAYER_SIZE_WIDTH;
            objectSize.Height = GlobalConstants.PLAYER_SIZE_HEIGHT;

            BaseSpeed = 0.7d;

            this.motionMaster = new ManualMovement(this);
        }

        private String pr_Name;
        /// <summary>
        /// Gets or Sets Player's name
        /// </summary>
        public String Name
        {
            get { return pr_Name; }
            set { pr_Name = value; }
        }

        /// <summary>
        /// Gets total game score of this Slug.
        /// </summary>
        public int Score { get { return this.score; } }

        /// <summary>
        /// Gets Count of Drops that this Slug has collected at current Level.
        /// </summary>
        public int CollectedDropsCount { get { return this.collectedDropsCount; } }

        private int pr_oozeEnergy;
        /// <summary>
        /// Gets or Sets Slug's Energy supply.
        /// </summary>
        public int OozeEnergy
        {
            get { return pr_oozeEnergy; }
            set
            {
                if (value > MaxOozeEnergy)
                    pr_oozeEnergy = MaxOozeEnergy;
                else if (value <= 0)
                {
                    pr_oozeEnergy = 0;
                    // Cast Deslimation when energy ends
                    CastEffect(4, this);
                }
                else
                    pr_oozeEnergy = value;
            }
        }

        /// <summary>
        /// Registers a Slug in <see cref="ObjectContainer"/>. (Overrides <see cref="Object.Create"/>.)
        /// </summary>
        public override void Create()
        {
            base.Create();

            // Bind Player's effect events to Game Form
            // Needed to display auras at AuraBar
            effectList.EffectApplied += new EffectCollection.EffectHandler(World.PlayForm.OnEffectApplied);
            effectList.EffectRemoved += new EffectCollection.EffectHandler(World.PlayForm.OnEffectRemoved);
        }

        public override void UpdateState(int timeP)
        {
            // Slug is moving:
            // take 2 OozeEnergy every second
            if (isInMotion && GetEffectsByType(EffectTypes.Replenishment).Count == 0) // or id under Replenishment effect
            {
                travelTime += timeP;
                if (travelTime > 1000)    // 1 second of motion
                {
                    travelTime -= 1000;
                    OozeEnergy -= 2;
                }

                isInMotion = false;
            }
            // Slug is standing still:
            // give 1 (3 at Start Point) OozeEnergy every second
            else
            {
                downTime += timeP;
                if (downTime > 1000)    // not in motion over 1 seconds
                {
                    downTime -= 1000;
                    OozeEnergy += IsAtHome ? 3 : 1;
                }
            }

            MovementAction(timeP);

            base.UpdateState(timeP);
        }

        public override void SetDeathState(DeathStates deathState)
        {
            if (deathState == DeathStates.Dead)
            {
                respawnTimer = 3000;
            }

            base.SetDeathState(deathState);
        }

        /// <summary>
        /// Player Moving Handler
        /// </summary>
        /// <param name="MoveType">Flags of direction</param>
        private void MovementAction(int timeP)
        {
            if (!IsAlive)
                return;

            if (HasEffectType(EffectTypes.Root))
                return;

            GPS previousPosition = Position;

            // Find a point in currectDirection + searchingStep
            // to determine whether Slug is affected by Slime speed-up boost
            GPS searchingPoint = Position;
            int searchingStep = 10;
            List<GridObject> slimeAround;
            bool slimePersist = false;

            switch (this.motionMaster.CurrentDirection.First)
            {
                case Directions.Right:
                    searchingPoint.X = Position.X + searchingStep;
                    if (searchingPoint.X > GlobalConstants.CELL_WIDTH)
                    {
                        searchingPoint.X -= GlobalConstants.CELL_WIDTH;
                        ++searchingPoint.Location.X;
                    }
                    break;
                case Directions.Left:
                    searchingPoint.X = Position.X - searchingStep;
                    if (searchingPoint.X < 0)
                    {
                        searchingPoint.X -= GlobalConstants.CELL_WIDTH;
                        --searchingPoint.Location.X;
                    }
                    break;
                case Directions.Up:
                    searchingPoint.Y = Position.Y - searchingStep;
                    if (searchingPoint.Y < 0)
                    {
                        searchingPoint.Y += GlobalConstants.CELL_HEIGHT;
                        --searchingPoint.Location.Y;
                    }
                    break;
                case Directions.Down:
                    searchingPoint.Y = Position.Y + searchingStep;
                    if (searchingPoint.Y > GlobalConstants.CELL_HEIGHT)
                    {
                        searchingPoint.Y -= GlobalConstants.CELL_HEIGHT;
                        ++searchingPoint.Location.Y;
                    }
                    break;
            }
            // try to find any Slime around that point
            slimeAround = ObjectSearcher.GetGridObjectsInArea(searchingPoint, searchingStep);
            foreach (GridObject slime in slimeAround)
            {
                if (slime.GridObjectType == GridObjectTypes.Slime)
                    slimePersist = true;
            }
            // Increase speed with an Effect "Viscous Slime - Slug"
            if (slimePersist)
                CastEffect(15, this);

            this.motionMaster.UpdateState(timeP);

            // Check movement occurrence
            if (previousPosition != Position)
            {
                this.isInMotion = true;

                //create slime at old position
                Slime slime = new Slime();
                slime.Create(previousPosition);
            }
            else
            {
                this.isInMotion = false;
            }
        }

        public void LevelChanged()
        {
            this.respawnLocation = Map.Instance.StartLocation;
            Position = new GPS(Home, 25, 25);
        }

        /// <summary>
        /// Handles process of picking up a <see cref="OozeDrop"/> object.
        /// </summary>
        /// <param name="drop"></param>
        public void CollectDrop(OozeDrop drop)
        {
            AddPoints(10);
            OozeEnergy += 10;
            Map.Instance.CollectDrop(drop);
            ++collectedDropsCount;
        }

        /// <summary>
        /// Adds points to Total <see cref="Slug.Score"/>
        /// </summary>
        /// <param name="points"></param>
        public void AddPoints(int points) { this.score += points; }

        /// <summary>
        /// Handles process of picking up a hidden bonus on map.
        /// </summary>
        /// <param name="effectID"><see cref="EffectEntry.ID"/> of the picking effect.</param>
        public void CollectHiddenBonus(ushort effectID)
        {
            EffectEntry effectEntry = DBStores.EffectStore[effectID];
            if(effectEntry.HasAttribute(EffectAttributes.CanBeSpell))
            {
                World.PlayForm.AddSpell(effectEntry);
            }
            else
            {
                Effect effect = new Effect(effectEntry, this, this);
                effect.Cast();
            }
        }

        /// <summary>
        /// Create an exact copy of the Slug at the same position and moving in the same direction as the original.
        /// </summary>
        public void CreateClone()
        {
            SlugClone clone = new SlugClone();
            clone.Create(Position, this.motionMaster.CurrentDirection);
        }

        protected override void UnitCollisionEnds(Unit unit)
        {
            if (this.HasEffectType(EffectTypes.Shield))
            {
                EffectHolder holder = this.effectList.GetHolder(11);
                if (holder != null)
                    RemoveEffect(holder);
            }
        }

        protected override void UnitCollision(Unit unit)
        {
            // Do not kill with shield effect
            if (!this.HasEffectType(EffectTypes.Shield))
                unit.KillUnit(this);
        }

    }
}
Back to Top