heli flight sortof ok

This commit is contained in:
Chris Forbes
2009-11-23 21:05:51 +13:00
parent c7e95c71b2
commit 7ccb2aa9ee
8 changed files with 112 additions and 29 deletions

View File

@@ -373,15 +373,24 @@ namespace OpenRa.Game
return;
unit = new Actor(name, (1 / 24f * producer.CenterLocation).ToInt2(), player);
var mobile = unit.traits.Get<Mobile>();
mobile.facing = 128;
var rp = producer.traits.GetOrDefault<RallyPoint>();
var dest = rp != null ? rp.rallyPoint : (unit.Location + new int2(0, 3));
var mobile = unit.traits.GetOrDefault<Mobile>();
if (mobile != null)
{
mobile.facing = 128;
mobile.QueueActivity(new Traits.Activities.Move(dest, 1));
}
var heli = unit.traits.GetOrDefault<Helicopter>();
if (heli != null)
{
heli.facing = 20;
heli.targetLocation = dest;
}
}
world.Add(unit);
player.FinishProduction(Rules.UnitCategory[name]);

View File

@@ -142,6 +142,7 @@
<Compile Include="Traits\AttackTurreted.cs" />
<Compile Include="Traits\Building.cs" />
<Compile Include="Traits\Harvester.cs" />
<Compile Include="Traits\Helicopter.cs" />
<Compile Include="Traits\InfantrySquad.cs" />
<Compile Include="Traits\McvDeploy.cs" />
<Compile Include="Traits\Mobile.cs" />

View File

@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRa.Game.GameRules;
namespace OpenRa.Game.Traits
{
class Helicopter : ITick, IOrder
{
public int facing;
public int altitude;
public int2 targetLocation;
const int CruiseAltitude = 20;
public Helicopter(Actor self)
{
targetLocation = self.Location;
}
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor)
{
if (lmb) return null;
if (underCursor == null)
return OpenRa.Game.Order.Move(self, xy,
!Game.IsCellBuildable(xy, UnitMovementType.Foot));
return null;
}
public void Tick(Actor self)
{
if (self.Location != targetLocation)
{
var dist = Game.CellSize * (targetLocation + new float2(.5f,.5f)) - self.CenterLocation;
var desiredFacing = Util.GetFacing(dist, facing);
Util.TickFacing(ref facing, desiredFacing,
self.unitInfo.ROT);
// .6f going the wrong way; .8f going sideways, 1f going forward.
var rawSpeed = .2f * (self.unitInfo as UnitInfo.VehicleInfo).Speed;
var angle = (facing - desiredFacing) / 128f * Math.PI;
var scale = .4f + .6f * (float)Math.Cos(angle);
if (altitude > CruiseAltitude / 2) // do some movement.
{
self.CenterLocation += (rawSpeed * scale / dist.Length) * dist;
self.CenterLocation += (1f - scale) * rawSpeed
* float2.FromAngle((float)angle);
self.Location = ((1 / 24f) * self.CenterLocation).ToInt2();
}
if (altitude < CruiseAltitude)
{
++altitude;
return;
}
}
else if (altitude > 0 &&
Game.IsCellBuildable( self.Location, UnitMovementType.Foot ))
{
--altitude;
}
/* todo: bob slightly when hovering */
}
}
}

View File

@@ -18,9 +18,12 @@ namespace OpenRa.Game.Traits
void PlayFacingAnim(Actor self)
{
var mobile = self.traits.GetOrDefault<Mobile>();
var heli = self.traits.GetOrDefault<Helicopter>();
anim.PlayFetchIndex("idle",
() => Util.QuantizeFacing(
self.traits.Get<Mobile>().facing,
mobile != null ? mobile.facing : heli.facing,
anim.CurrentSequence.Length ));
}

View File

@@ -25,8 +25,6 @@ namespace OpenRa.Game.Traits
public override IEnumerable<Tuple<Sprite, float2, int>> Render(Actor self)
{
var mobile = self.traits.Get<Mobile>();
yield return Util.CenteredShadow(self, anim.Image, self.CenterLocation);
yield return Util.CenteredShadow(self, rotorAnim.Image, self.CenterLocation
+ Util.GetTurretPosition(self, self.unitInfo.PrimaryOffset, 0));
@@ -34,7 +32,8 @@ namespace OpenRa.Game.Traits
yield return Util.CenteredShadow(self, (secondRotorAnim ?? rotorAnim).Image, self.CenterLocation
+ Util.GetTurretPosition(self, self.unitInfo.SecondaryOffset, 0));
var p = self.CenterLocation - new float2( 0, altitude );
var heli = self.traits.Get<Helicopter>();
var p = self.CenterLocation - new float2( 0, heli.altitude );
yield return Util.Centered(self, anim.Image, p);
yield return Util.Centered(self, rotorAnim.Image, p
@@ -44,10 +43,6 @@ namespace OpenRa.Game.Traits
+ Util.GetTurretPosition(self, self.unitInfo.SecondaryOffset, 0));
}
int altitude = 0;
const int climbRate = 1;
const int cruiseAltitude = 20;
public override void Tick(Actor self)
{
base.Tick(self);
@@ -55,20 +50,17 @@ namespace OpenRa.Game.Traits
if (secondRotorAnim != null)
secondRotorAnim.Tick();
var mobile = self.traits.Get<Mobile>();
var isFlying = mobile.HasActivity;
var heli = self.traits.GetOrDefault<Helicopter>();
if (heli == null) return;
if (isFlying && altitude < cruiseAltitude)
altitude = Math.Min(altitude + climbRate, cruiseAltitude);
else if (!isFlying && altitude > 0)
altitude = Math.Max(altitude - climbRate, 0);
var isFlying = heli.altitude > 0;
if ((altitude >0) ^ (rotorAnim.CurrentSequence.Name != "rotor"))
if (isFlying ^ (rotorAnim.CurrentSequence.Name != "rotor"))
return;
rotorAnim.PlayRepeatingPreservingPosition((altitude>0) ? "rotor" : "slow-rotor");
rotorAnim.PlayRepeatingPreservingPosition(isFlying ? "rotor" : "slow-rotor");
if (secondRotorAnim != null)
secondRotorAnim.PlayRepeatingPreservingPosition((altitude>0) ? "rotor2" : "slow-rotor2");
secondRotorAnim.PlayRepeatingPreservingPosition(isFlying ? "rotor2" : "slow-rotor2");
}
}
}

View File

@@ -91,7 +91,7 @@ namespace OpenRa.Game.Traits
var ru = self.traits.WithInterface<RenderUnit>().FirstOrDefault();
if (ru == null) return int2.Zero; /* things that don't have a rotating base don't need the turrets repositioned */
var bodyFacing = self.traits.Get<Mobile>().facing;
var bodyFacing = self.traits.Contains<Mobile>() ? self.traits.Get<Mobile>().facing : self.traits.Get<Helicopter>().facing;
var quantizedFacing = QuantizeFacing(bodyFacing, ru.anim.CurrentSequence.Length) * (256 / ru.anim.CurrentSequence.Length);
return (RotateVectorByFacing(new float2(offset[0], offset[1]), quantizedFacing, .7f) + GetRecoil(self, recoil))

View File

@@ -15,9 +15,16 @@ namespace OpenRa.Game
{
case "Move":
{
var mobile = order.Subject.traits.Get<Mobile>();
mobile.Cancel( order.Subject );
mobile.QueueActivity( new Traits.Activities.Move( order.TargetLocation, 8 ) );
var mobile = order.Subject.traits.GetOrDefault<Mobile>();
if (mobile != null)
{
mobile.Cancel(order.Subject);
mobile.QueueActivity(new Traits.Activities.Move(order.TargetLocation, 8));
}
var heli = order.Subject.traits.GetOrDefault<Helicopter>();
if (heli != null)
heli.targetLocation = order.TargetLocation;
var attackBase = order.Subject.traits.WithInterface<AttackBase>().FirstOrDefault();
if( attackBase != null )

View File

@@ -128,16 +128,17 @@ Description=Transport Helicopter
PrimaryOffset=0,14,0,-4
SecondaryOffset=0,-14,0,-2
BuiltAt=hpad
Traits=Mobile, RenderUnitRotor
Traits=Helicopter, RenderUnitRotor
SecondaryAnim=rotor2
[HELI]
Description=Longbow
BuiltAt=hpad
Traits=Mobile, RenderUnitRotor
Traits=Helicopter, RenderUnitRotor
PrimaryOffset=0,0,0,-2
[HIND]
Description=Hind
BuiltAt=hpad
Traits=Mobile, RenderUnitRotor
Traits=Helicopter, RenderUnitRotor