Skip to content
This repository has been archived by the owner on May 9, 2018. It is now read-only.

Commit

Permalink
Further fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
GraionDilach committed Nov 7, 2016
1 parent f61923f commit 6d8dd2e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
58 changes: 39 additions & 19 deletions Traits/GrantPeriodicUpgrades.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class GrantPeriodicUpgradesInfo : UpgradableTraitInfo, Requires<UpgradeMa
public readonly Color CooldownColor = Color.DarkRed;
public readonly Color ActiveColor = Color.DarkMagenta;

public object Create(ActorInitializer init) { return new GrantPeriodicUpgrades(init, this); }
public override object Create(ActorInitializer init) { return new GrantPeriodicUpgrades(init, this); }
}

public class GrantPeriodicUpgrades : UpgradableTrait<GrantPeriodicUpgradesInfo>, INotifyCreated, ISelectionBar, ITick
Expand All @@ -47,16 +47,14 @@ public class GrantPeriodicUpgrades : UpgradableTrait<GrantPeriodicUpgradesInfo>,

[Sync] int ticks;
int cooldown, active;
bool isEnabled;
bool isEnabled, isSuspended;

public GrantPeriodicUpgrades(ActorInitializer init, GrantPeriodicUpgradesInfo info)
: base (info)
: base(info)
{
self = init.Self;
this.info = info;
manager = self.Trait<UpgradeManager>();

SetDefaultState();
}

void SetDefaultState()
Expand All @@ -67,23 +65,24 @@ void SetDefaultState()
? self.World.SharedRandom.Next(info.ActiveDuration[0], info.ActiveDuration[1])
: info.ActiveDuration[0];
active = ticks;
isEnabled = true;
if (info.StartsGranted != isEnabled)
EnableUpgrade();
}
else
{
ticks = info.CooldownDuration.Length == 2
? self.World.SharedRandom.Next(info.CooldownDuration[0], info.CooldownDuration[1])
: info.CooldownDuration[0];
cooldown = ticks;
isEnabled = false;
if (info.StartsGranted != isEnabled)
DisableUpgrade();
}
}

void INotifyCreated.Created(Actor self)
{
if (isEnabled)
foreach (var up in info.Upgrades)
manager.GrantUpgrade(self, up, this);
if (!IsTraitDisabled)
SetDefaultState();
}

void ITick.Tick(Actor self)
Expand All @@ -92,37 +91,58 @@ void ITick.Tick(Actor self)
{
if (isEnabled)
{
foreach (var up in info.Upgrades)
manager.RevokeUpgrade(self, up, this);

ticks = info.CooldownDuration.Length == 2
? self.World.SharedRandom.Next(info.CooldownDuration[0], info.CooldownDuration[1])
: info.CooldownDuration[0];
cooldown = ticks;
isEnabled = false;
DisableUpgrade();
}
else
{
foreach (var up in info.Upgrades)
manager.GrantUpgrade(self, up, this);

ticks = info.ActiveDuration.Length == 2
? self.World.SharedRandom.Next(info.ActiveDuration[0], info.ActiveDuration[1])
: info.ActiveDuration[0];
active = ticks;
isEnabled = true;
EnableUpgrade();
}
}
}

protected override void UpgradeEnabled(Actor self)
{
if (info.ResetTraitOnEnable)
{
SetDefaultState();
else if (isSuspended)
EnableUpgrade();

isSuspended = false;
}

protected override void UpgradeDisabled(Actor self)
{
if (isEnabled)
{
DisableUpgrade();
isSuspended = true;
}
}

void EnableUpgrade()
{
foreach (var up in info.Upgrades)
manager.GrantUpgrade(self, up, this);

isEnabled = true;
}

void DisableUpgrade()
{
foreach (var up in info.Upgrades)
manager.RevokeUpgrade(self, up, this);

isEnabled = false;
}

float ISelectionBar.GetValue()
{
if (!info.ShowSelectionBar)
Expand Down
8 changes: 3 additions & 5 deletions Traits/SmokeParticleEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public class SmokeParticleEmitterInfo : UpgradableTraitInfo

public class SmokeParticleEmitter : UpgradableTrait<SmokeParticleEmitterInfo>, ITick
{
readonly WPos spawnpos;
readonly MersenneTwister random;
readonly WVec offset;

int ticks;

Expand All @@ -50,11 +50,9 @@ public SmokeParticleEmitter(Actor self, SmokeParticleEmitterInfo info)
{
random = self.World.SharedRandom;

var offset = Info.Offset.Length == 2
offset = Info.Offset.Length == 2
? new WVec(random.Next(Info.Offset[0].X, Info.Offset[1].X), random.Next(Info.Offset[0].Y, Info.Offset[1].Y), random.Next(Info.Offset[0].Z, Info.Offset[1].Z))
: Info.Offset[0];

spawnpos = self.CenterPosition + offset;
}

public void Tick(Actor self)
Expand All @@ -66,7 +64,7 @@ public void Tick(Actor self)
{
ticks = Info.SpawnFrequency.Length == 2 ? random.Next(Info.SpawnFrequency[0], Info.SpawnFrequency[1]) : Info.SpawnFrequency[0];

self.World.AddFrameEndTask(w => w.Add(new SmokeParticle(spawnpos, Info.Gravity, w, Info.Image, Info.Sequence, Info.Palette, false, false)));
self.World.AddFrameEndTask(w => w.Add(new SmokeParticle(self.CenterPosition + offset, Info.Gravity, w, Info.Image, Info.Sequence, Info.Palette, false, false)));
}
}
}
Expand Down

0 comments on commit 6d8dd2e

Please sign in to comment.