Rewrite RenderCargo -> WithCargo.

Now uses world coordinates and properly displays
cargo at all facings.
This commit is contained in:
Paul Chote
2013-05-15 00:58:10 +12:00
parent 462478afdf
commit 9b7aaebcbc
6 changed files with 84 additions and 77 deletions

View File

@@ -52,15 +52,6 @@ namespace OpenRA.Graphics
public Renderable WithZOffset(int newOffset) { return new Renderable(Sprite, Pos, newOffset, Palette, Scale); }
public Renderable WithPos(WPos pos) { return new Renderable(Sprite, pos, ZOffset, Palette, Scale); }
// Transitional hack
public Renderable WithPxOffset(float2 offset)
{
var x = (int)(Pos.X * Game.CellSize / 1024 + offset.X);
var y = (int)(Pos.Y * Game.CellSize / 1024 + offset.Y);
var z = (int)(Pos.Z * Game.CellSize / 1024);
return new Renderable(Sprite, new PPos(x,y).ToWPos(z), ZOffset, Palette, Scale);
}
public void Render(WorldRenderer wr)
{
Sprite.DrawAt(wr.ScreenPxPosition(Pos) - 0.5f*Scale*Sprite.size, Palette.Index, Scale);

View File

@@ -86,7 +86,7 @@
<Compile Include="ProductionQueueFromSelection.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RenderBuildingRefinery.cs" />
<Compile Include="RenderCargo.cs" />
<Compile Include="WithCargo.cs" />
<Compile Include="RenderGunboat.cs" />
<Compile Include="SpawnViceroid.cs" />
<Compile Include="TiberiumRefinery.cs" />

View File

@@ -1,64 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 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.RA;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc
{
public class RenderCargoInfo : ITraitInfo, Requires<CargoInfo>
{
/* altitude of the cargo, relative to us. -ve is underneath us */
public readonly int RelativeAltitude = 0;
public readonly string[] PassengerTypes;
public object Create(ActorInitializer init) { return new RenderCargo(init.self, this); }
}
public class RenderCargo : IRenderModifier
{
Cargo cargo;
IFacing facing;
IMove move;
RenderCargoInfo Info;
public RenderCargo(Actor self, RenderCargoInfo info)
{
cargo = self.Trait<Cargo>();
facing = self.TraitOrDefault<IFacing>();
move = self.Trait<IMove>();
Info = info;
}
public IEnumerable<Renderable> ModifyRender(Actor self, WorldRenderer wr, IEnumerable<Renderable> r)
{
foreach (var c in cargo.Passengers)
{
c.Trait<ITeleportable>().SetPxPosition( c, move.PxPosition );
var cargoFacing = c.TraitOrDefault<IFacing>();
if (facing != null && cargoFacing != null)
cargoFacing.Facing = facing.Facing;
}
var visiblePassengers = (Info.PassengerTypes != null && Info.PassengerTypes.Length > 0)
? cargo.Passengers.Where(p =>
Info.PassengerTypes.Contains(p.Trait<Passenger>().info.CargoType))
: cargo.Passengers;
return r.Concat(visiblePassengers.SelectMany(a => a.Render(wr))
.Select(a => a.WithPxOffset(new float2(0, -Info.RelativeAltitude))
.WithZOffset(a.ZOffset + Info.RelativeAltitude)));
}
}
}

View File

@@ -0,0 +1,78 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 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.FileFormats;
using OpenRA.Graphics;
using OpenRA.Mods.RA;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc
{
public class WithCargoInfo : ITraitInfo, Requires<CargoInfo>, Requires<LocalCoordinatesModelInfo>
{
[Desc("Cargo position relative to turret or body. (forward, right, up) triples")]
public readonly WRange[] LocalOffset = {};
public readonly string[] DisplayTypes = {};
public object Create(ActorInitializer init) { return new WithCargo(init.self, this); }
}
public class WithCargo : IRenderModifier
{
Cargo cargo;
IFacing facing;
WithCargoInfo Info;
WVec[] positions;
ILocalCoordinatesModel coords;
public WithCargo(Actor self, WithCargoInfo info)
{
cargo = self.Trait<Cargo>();
facing = self.TraitOrDefault<IFacing>();
Info = info;
coords = self.Trait<ILocalCoordinatesModel>();
if (info.LocalOffset.Length % 3 != 0)
throw new InvalidOperationException("Invalid LocalOffset array length");
positions = new WVec[info.LocalOffset.Length / 3];
for (var i = 0; i < info.LocalOffset.Length / 3; i++)
positions[i] = new WVec(info.LocalOffset[3*i], info.LocalOffset[3*i + 1], info.LocalOffset[3*i + 2]);
}
public IEnumerable<Renderable> ModifyRender(Actor self, WorldRenderer wr, IEnumerable<Renderable> r)
{
foreach (var rr in r)
yield return rr;
var bodyOrientation = coords.QuantizeOrientation(self, self.Orientation);
var pos = self.CenterPosition;
int i = 0;
foreach (var c in cargo.Passengers)
{
var cargoFacing = c.TraitOrDefault<IFacing>();
if (facing != null && cargoFacing != null)
cargoFacing.Facing = facing.Facing;
var cargoPassenger = c.Trait<Passenger>();
if (Info.DisplayTypes.Contains(cargoPassenger.info.CargoType))
{
var offset = pos - c.CenterPosition + coords.LocalToWorld(positions[i++ % positions.Length].Rotate(bodyOrientation));
foreach (var cr in c.Render(wr))
yield return cr.WithPos(cr.Pos + offset).WithZOffset(1);
}
}
}
}
}

View File

@@ -56,7 +56,9 @@ LST:
Range: 7
RenderUnit:
WithRoof:
RenderCargo:
WithCargo:
DisplayTypes: Infantry, Vehicle
LocalOffset: 0,0,0, -390,-256,0, 390,-256,0, -390,256,0, 390,256,0
-Selectable:
Cargo:
Types: Infantry, Vehicle

View File

@@ -21,8 +21,8 @@
RearmBuildings: starporta,starporto,starporth
MinimalLandAltitude: 25
RenderUnit:
RenderCargo:
RelativeAltitude: 20
WithCargo:
LocalOffset: 0,0,-854
WithShadow:
Cargo:
Types: Vehicle