Move Targetable*, Seeds/StoresResource(s) and two RenderBuilding traits to Mods.Common

This commit is contained in:
reaperrr
2015-01-04 17:06:33 +01:00
parent b7a3b9fdbf
commit 7a8826f5e0
9 changed files with 15 additions and 21 deletions

View File

@@ -1,41 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using OpenRA.Mods.RA;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Traits
{
public class TargetableAircraftInfo : TargetableUnitInfo
{
public readonly string[] GroundedTargetTypes = { };
public override object Create(ActorInitializer init) { return new TargetableAircraft(init.Self, this); }
}
public class TargetableAircraft : TargetableUnit
{
readonly TargetableAircraftInfo info;
readonly Actor self;
public TargetableAircraft(Actor self, TargetableAircraftInfo info)
: base(self, info)
{
this.info = info;
this.self = self;
}
public override string[] TargetTypes
{
get { return (self.CenterPosition.Z > 0) ? info.TargetTypes
: info.GroundedTargetTypes; }
}
}
}

View File

@@ -1,49 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Traits
{
public class TargetableBuildingInfo : ITraitInfo, ITargetableInfo, Requires<BuildingInfo>
{
public readonly string[] TargetTypes = { };
public string[] GetTargetTypes() { return TargetTypes; }
public bool RequiresForceFire = false;
public object Create(ActorInitializer init) { return new TargetableBuilding(init.Self, this); }
}
public class TargetableBuilding : ITargetable
{
readonly TargetableBuildingInfo info;
readonly Building building;
public TargetableBuilding(Actor self, TargetableBuildingInfo info)
{
this.info = info;
building = self.Trait<Building>();
}
public string[] TargetTypes { get { return info.TargetTypes; } }
public bool TargetableBy(Actor self, Actor byActor) { return true; }
public IEnumerable<WPos> TargetablePositions(Actor self)
{
return building.OccupiedCells().Select(c => self.World.Map.CenterOfCell(c.First));
}
public bool RequiresForceFire { get { return info.RequiresForceFire; } }
}
}

View File

@@ -1,87 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Support;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Traits
{
[Desc("Lets the actor spread resources around it in a circle.")]
class SeedsResourceInfo : ITraitInfo
{
public readonly int Interval = 75;
public readonly string ResourceType = "Ore";
public readonly int MaxRange = 100;
public object Create(ActorInitializer init) { return new SeedsResource(init.Self, this); }
}
class SeedsResource : ITick, ISeedableResource
{
readonly SeedsResourceInfo info;
readonly ResourceType resourceType;
readonly ResourceLayer resLayer;
public SeedsResource(Actor self, SeedsResourceInfo info)
{
this.info = info;
resourceType = self.World.WorldActor.TraitsImplementing<ResourceType>()
.FirstOrDefault(t => t.Info.Name == info.ResourceType);
if (resourceType == null)
throw new InvalidOperationException("No such resource type `{0}`".F(info.ResourceType));
resLayer = self.World.WorldActor.Trait<ResourceLayer>();
}
int ticks;
public void Tick(Actor self)
{
if (--ticks <= 0)
{
Seed(self);
ticks = info.Interval;
}
}
public void Seed(Actor self)
{
var cell = RandomWalk(self.Location, self.World.SharedRandom)
.Take(info.MaxRange)
.SkipWhile(p => resLayer.GetResource(p) == resourceType && resLayer.IsFull(p))
.Cast<CPos?>().FirstOrDefault();
if (cell != null && resLayer.CanSpawnResourceAt(resourceType, cell.Value))
resLayer.AddResource(resourceType, cell.Value, 1);
}
static IEnumerable<CPos> RandomWalk(CPos p, MersenneTwister r)
{
for (;;)
{
var dx = r.Next(-1, 2);
var dy = r.Next(-1, 2);
if (dx == 0 && dy == 0)
continue;
p += new CVec(dx, dy);
yield return p;
}
}
}
}

View File

@@ -1,57 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Traits
{
[Desc("Actor can be targeted.")]
public class TargetableUnitInfo : ITraitInfo, ITargetableInfo
{
[Desc("Target type. Used for filtering (in)valid targets.")]
public readonly string[] TargetTypes = { };
public string[] GetTargetTypes() { return TargetTypes; }
public bool RequiresForceFire = false;
public virtual object Create(ActorInitializer init) { return new TargetableUnit(init.Self, this); }
}
public class TargetableUnit : ITargetable
{
readonly TargetableUnitInfo info;
protected Cloak cloak;
public TargetableUnit(Actor self, TargetableUnitInfo info)
{
this.info = info;
cloak = self.TraitOrDefault<Cloak>();
}
public virtual bool TargetableBy(Actor self, Actor viewer)
{
if (cloak == null || !cloak.Cloaked)
return true;
return cloak.IsVisible(self, viewer.Owner);
}
public virtual string[] TargetTypes { get { return info.TargetTypes; } }
public virtual IEnumerable<WPos> TargetablePositions(Actor self)
{
yield return self.CenterPosition;
}
public bool RequiresForceFire { get { return info.RequiresForceFire; } }
}
}