Improvements to Mobile to support smooth movement (like real-ra does it)

- McvDeploy got simpler.
    - BUGFIX: Bullet no longer crashes when it damages a tree
This commit is contained in:
Bob
2009-10-24 17:28:51 +13:00
parent 979bb74bba
commit 06e6d50735
5 changed files with 255 additions and 171 deletions

View File

@@ -4,19 +4,19 @@ using System.Linq;
using System.Text; using System.Text;
using OpenRa.Game.GameRules; using OpenRa.Game.GameRules;
using IjwFramework.Types; using IjwFramework.Types;
using OpenRa.Game.Graphics; using OpenRa.Game.Graphics;
namespace OpenRa.Game namespace OpenRa.Game
{ {
interface IEffect interface IEffect
{ {
void Tick(); void Tick();
IEnumerable<Pair<Sprite, float2>> Render(); IEnumerable<Pair<Sprite, float2>> Render();
Player Owner { get; } Player Owner { get; }
} }
class Bullet : IEffect class Bullet : IEffect
{ {
public Player Owner { get; private set; } public Player Owner { get; private set; }
readonly Actor FiredBy; readonly Actor FiredBy;
readonly WeaponInfo Weapon; readonly WeaponInfo Weapon;
@@ -49,22 +49,22 @@ namespace OpenRa.Game
int TotalTime() { return (Dest - Src).Length * BaseBulletSpeed / Weapon.Speed; } int TotalTime() { return (Dest - Src).Length * BaseBulletSpeed / Weapon.Speed; }
public void Tick() public void Tick()
{ {
if (t == 0) if (t == 0)
Game.PlaySound(Weapon.Report + ".aud", false); Game.PlaySound(Weapon.Report + ".aud", false);
t += 40; t += 40;
if (t > TotalTime()) /* remove finished bullets */ if (t > TotalTime()) /* remove finished bullets */
{ {
Game.world.AddFrameEndTask(w => w.Remove(this)); Game.world.AddFrameEndTask(w => w.Remove(this));
Game.world.AddFrameEndTask(w => w.Add(new Explosion(Dest))); Game.world.AddFrameEndTask(w => w.Add(new Explosion(Dest)));
var maxSpread = GetMaximumSpread(); var maxSpread = GetMaximumSpread();
var hitActors = Game.FindUnitsInCircle(Dest, GetMaximumSpread()); var hitActors = Game.FindUnitsInCircle(Dest, GetMaximumSpread());
foreach (var victim in hitActors) foreach (var victim in hitActors)
victim.InflictDamage(FiredBy, this, (int)GetDamageToInflict(victim)); victim.InflictDamage(FiredBy, this, (int)GetDamageToInflict(victim));
} }
} }
@@ -75,21 +75,24 @@ namespace OpenRa.Game
Src.ToFloat2(), Src.ToFloat2(),
Dest.ToFloat2(), Dest.ToFloat2(),
(float)t / TotalTime()) - 0.5f * anim.Image.size); (float)t / TotalTime()) - 0.5f * anim.Image.size);
} }
float GetMaximumSpread() float GetMaximumSpread()
{
return (int)(Warhead.Spread * Math.Log(Weapon.Damage, 2));
}
float GetDamageToInflict(Actor target)
{ {
return (int)(Warhead.Spread * Math.Log(Weapon.Damage, 2)); if( target.unitInfo == null ) // tree or other doodad
} return 0;
float GetDamageToInflict(Actor target) /* todo: some things can't be damaged AT ALL by certain weapons! */
{ var distance = (target.CenterLocation - Dest).Length;
/* todo: some things can't be damaged AT ALL by certain weapons! */ var rawDamage = Weapon.Damage * (float)Math.Exp(-distance / Warhead.Spread);
var distance = (target.CenterLocation - Dest).Length; var multiplier = Warhead.EffectivenessAgainst(target.unitInfo.Armor);
var rawDamage = Weapon.Damage * (float)Math.Exp(-distance / Warhead.Spread);
var multiplier = Warhead.EffectivenessAgainst(target.unitInfo.Armor); return rawDamage * multiplier;
return rawDamage * multiplier;
} }
} }
} }

View File

@@ -18,12 +18,12 @@ namespace OpenRa.Game
{ {
this.Unit = unit; this.Unit = unit;
this.Destination = destination; this.Destination = destination;
} }
string GetVoiceSuffix() string GetVoiceSuffix()
{ {
var suffixes = new[] { ".r01", ".r03" }; var suffixes = new[] { ".r01", ".r03" };
return suffixes[Unit.traits.Get<Traits.Mobile>().Voice]; return suffixes[Unit.traits.Get<Traits.Mobile>().Voice];
} }
public override void Apply( bool leftMouseButton ) public override void Apply( bool leftMouseButton )
@@ -31,31 +31,9 @@ namespace OpenRa.Game
if (leftMouseButton) return; if (leftMouseButton) return;
if (Game.LocalPlayer == Unit.Owner) if (Game.LocalPlayer == Unit.Owner)
Game.PlaySound(Game.SovietVoices.First.GetNext() + GetVoiceSuffix(), false); Game.PlaySound("ackno.r00", false);
var mobile = Unit.traits.Get<Traits.Mobile>(); var mobile = Unit.traits.Get<Traits.Mobile>();
mobile.destination = Destination; mobile.SetNextAction( new Traits.Mobile.MoveTo( Destination ) );
mobile.desiredFacing = null;
}
}
class DeployMcvOrder : Order
{
Actor Unit;
int2 Location;
public DeployMcvOrder( Actor unit, int2 location )
{
Unit = unit;
Location = location;
}
public override void Apply( bool leftMouseButton )
{
if (leftMouseButton) return;
Unit.traits.Get<Traits.McvDeploy>().DeployLocation = Location;
var mobile = Unit.traits.Get<Traits.Mobile>();
mobile.destination = mobile.toCell;
} }
} }
} }

View File

@@ -5,42 +5,51 @@ using System.Text;
namespace OpenRa.Game.Traits namespace OpenRa.Game.Traits
{ {
class McvDeploy : IOrder, ITick class McvDeploy : IOrder
{ {
public int2? DeployLocation;
public McvDeploy(Actor self) public McvDeploy(Actor self)
{ {
} }
public Order Order(Actor self, int2 xy) public Order Order(Actor self, int2 xy)
{ {
DeployLocation = null;
// TODO: check that there's enough space at the destination. // TODO: check that there's enough space at the destination.
if( xy == self.Location ) if( xy == self.Location )
return new DeployMcvOrder( self, xy ); return new DeployMcvOrder( self, xy );
return null; return null;
} }
}
public void Tick(Actor self) class DeployMcvOrder : Order
{
Actor Unit;
int2 Location;
public DeployMcvOrder( Actor unit, int2 location )
{ {
if( self.Location != DeployLocation ) Unit = unit;
return; Location = location;
}
var mobile = self.traits.Get<Mobile>(); public override void Apply( bool leftMouseButton )
mobile.desiredFacing = 96; {
if( mobile.moveFraction < mobile.moveFractionTotal ) if( leftMouseButton ) return;
return; Unit.traits.Get<Mobile>().SetNextAction( new Mobile.Turn( 96 ) { NextAction = new DeployAction() } );
}
if( mobile.facing != mobile.desiredFacing ) class DeployAction : Mobile.CurrentAction
return; {
public Mobile.CurrentAction NextAction { get; set; }
Game.world.AddFrameEndTask(_ => public void Tick( Actor self, Mobile mobile )
{ {
Game.world.Remove(self); Game.world.AddFrameEndTask( _ =>
Game.world.Add(new Actor("fact", self.Location - new int2(1, 1), self.Owner)); {
}); Game.world.Remove( self );
Game.world.Add( new Actor( "fact", self.Location - new int2( 1, 1 ), self.Owner ) );
} );
}
} }
} }
} }

View File

@@ -1,84 +1,181 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using OpenRa.Game.GameRules; using OpenRa.Game.GameRules;
using OpenRa.Game.Graphics; using OpenRa.Game.Graphics;
namespace OpenRa.Game.Traits namespace OpenRa.Game.Traits
{ {
class Mobile : ITick, IOrder class Mobile : ITick, IOrder
{ {
public Actor self; public Actor self;
public int2 fromCell, destination; public int2 fromCell;
public int2 toCell { get { return self.Location; } } public int2 toCell { get { return self.Location; } set { self.Location = value; } }
public int moveFraction, moveFractionTotal; public int facing;
public int facing;
public int? desiredFacing; public int Voice = Game.CosmeticRandom.Next(2);
public int Voice = Game.CosmeticRandom.Next(2); CurrentAction currentAction;
public Mobile(Actor self) public Mobile(Actor self)
{ {
this.self = self; this.self = self;
fromCell = destination = self.Location; fromCell = toCell;
} }
void UpdateCenterLocation() public void SetNextAction( CurrentAction nextAction )
{ {
float fraction = (moveFraction > 0) ? (float)moveFraction / moveFractionTotal : 0f; if( currentAction == null )
self.CenterLocation = new float2(12, 12) + Game.CellSize * float2.Lerp(fromCell, toCell, fraction); currentAction = nextAction;
} else
currentAction.NextAction = nextAction;
public void Tick(Actor self) }
{
Move(self); public void Tick(Actor self)
UpdateCenterLocation(); {
} if( currentAction != null )
currentAction.Tick( self, this );
void Move(Actor self) else
{ fromCell = toCell;
if( fromCell != toCell ) }
desiredFacing = Util.GetFacing( toCell - fromCell, facing );
public Order Order(Actor self, int2 xy)
if( desiredFacing != null && desiredFacing != facing ) {
{ if (xy != toCell)
Util.TickFacing( ref facing, desiredFacing.Value, self.unitInfo.ROT ); return new MoveOrder(self, xy);
return;
} return null;
desiredFacing = null; }
if( fromCell != toCell )
moveFraction += ((UnitInfo.MobileInfo)self.unitInfo).Speed; public interface CurrentAction
{
if (moveFraction < moveFractionTotal) CurrentAction NextAction { set; }
return; void Tick( Actor self, Mobile mobile );
}
moveFraction = 0;
moveFractionTotal = 0; public class Turn : CurrentAction
fromCell = toCell; {
public CurrentAction NextAction { get; set; }
if (destination == toCell)
return; public readonly int desiredFacing;
List<int2> res = Game.pathFinder.FindUnitPath(toCell, PathFinder.DefaultEstimator(destination)); public Turn( int desiredFacing )
if (res.Count != 0) {
{ this.desiredFacing = desiredFacing;
self.Location = res[res.Count - 1]; }
int2 dir = toCell - fromCell; public void Tick( Actor self, Mobile mobile )
moveFractionTotal = (dir.X != 0 && dir.Y != 0) ? 70 : 50; {
} if( desiredFacing == mobile.facing )
else {
destination = toCell; mobile.currentAction = NextAction;
} if( NextAction != null )
NextAction.Tick( self, mobile );
public Order Order(Actor self, int2 xy) return;
{ }
if (xy != toCell) Util.TickFacing( ref mobile.facing, desiredFacing, self.unitInfo.ROT );
return new MoveOrder(self, xy); }
}
return null;
} public class MoveTo : CurrentAction
} {
} public CurrentAction NextAction { get; set; }
int2 destination;
List<int2> path;
int moveFraction, moveFractionTotal;
float2 from, to;
Action<Actor, Mobile> OnComplete;
public MoveTo( int2 destination )
{
this.destination = destination;
}
public void Tick( Actor self, Mobile mobile )
{
if( moveFractionTotal != 0 )
{
TickMove( self, mobile );
return;
}
if( destination == self.Location )
{
mobile.currentAction = NextAction;
return;
}
if( path == null )
path = Game.pathFinder.FindUnitPath( self.Location, PathFinder.DefaultEstimator( destination ) );
if( path.Count == 0 )
{
destination = mobile.toCell;
return;
}
var nextCell = path[ path.Count - 1 ];
int2 dir = nextCell - mobile.fromCell;
var firstFacing = Util.GetFacing( dir, mobile.facing );
if( firstFacing != mobile.facing )
mobile.currentAction = new Turn( firstFacing ) { NextAction = this };
else
{
mobile.toCell = nextCell;
path.RemoveAt( path.Count - 1 );
moveFractionTotal = ( dir.X != 0 && dir.Y != 0 ) ? 35 : 25;
from = CenterOfCell( mobile.fromCell );
to = BetweenCells( mobile.fromCell, mobile.toCell );
OnComplete = OnCompleteFirstHalf;
}
mobile.currentAction.Tick( self, mobile );
}
void TickMove( Actor self, Mobile mobile )
{
moveFraction += ( self.unitInfo as UnitInfo.MobileInfo ).Speed;
UpdateCenterLocation( self, (float)moveFraction / moveFractionTotal, from, to );
if( moveFraction >= moveFractionTotal )
{
moveFraction -= moveFractionTotal;
OnComplete( self, mobile );
mobile.fromCell = mobile.toCell;
}
return;
}
static void UpdateCenterLocation( Actor self, float frac, float2 from, float2 to )
{
self.CenterLocation = float2.Lerp( from, to, frac );
}
static float2 CenterOfCell( int2 loc )
{
return new float2( 12, 12 ) + Game.CellSize * (float2)loc;
}
static float2 BetweenCells( int2 from, int2 to )
{
return 0.5f * ( CenterOfCell( from ) + CenterOfCell( to ) );
}
void OnCompleteFirstHalf( Actor self, Mobile mobile )
{
from = BetweenCells( mobile.fromCell, mobile.toCell );
to = CenterOfCell( mobile.toCell );
OnComplete = OnCompleteSecondHalf;
}
void OnCompleteSecondHalf( Actor self, Mobile mobile )
{
moveFractionTotal = 0;
self.CenterLocation = CenterOfCell( mobile.toCell );
OnComplete = null;
}
}
}
}

View File

@@ -23,10 +23,7 @@ namespace OpenRa.Game.Traits
public override IEnumerable<Pair<Sprite, float2>> Render(Actor self) public override IEnumerable<Pair<Sprite, float2>> Render(Actor self)
{ {
var mobile = self.traits.Get<Mobile>(); yield return Centered( anim.Image, self.CenterLocation );
float fraction = (mobile.moveFraction > 0) ? (float)mobile.moveFraction / mobile.moveFractionTotal : 0f;
var centerLocation = new float2(12, 12) + Game.CellSize * float2.Lerp(mobile.fromCell, mobile.toCell, fraction);
yield return Centered(anim.Image, centerLocation);
} }
} }
} }