added AttackTurreted.

This commit is contained in:
Bob
2009-10-18 23:51:00 +13:00
parent 62207efbd9
commit 5f2d89fd63
8 changed files with 144 additions and 54 deletions

View File

@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Game.Traits
{
class AttackTurreted : ITick
{
public Actor target;
// time (in frames) until each weapon can fire again.
int primaryFireDelay = 0;
int secondaryFireDelay = 0;
public AttackTurreted( Actor self )
{
self.traits.Get<Turreted>();
}
public void Tick( Actor self, Game game, int dt )
{
if( primaryFireDelay > 0 )
--primaryFireDelay;
if( secondaryFireDelay > 0 )
--secondaryFireDelay;
if( target == null )
return;
var mobile = self.traits.Get<Mobile>();
var turreted = self.traits.Get<Turreted>();
turreted.desiredFacing = mobile.GetFacing( target.Location - self.Location );
if( turreted.desiredFacing != turreted.turretFacing )
return;
if( self.unitInfo.Primary != null && CheckFire( self, game, self.unitInfo.Primary, ref primaryFireDelay ) )
{
secondaryFireDelay = Math.Max( 4, secondaryFireDelay );
return;
}
if( self.unitInfo.Secondary != null && CheckFire( self, game, self.unitInfo.Secondary, ref secondaryFireDelay ) )
return;
}
bool CheckFire( Actor self, Game game, string weaponName, ref int fireDelay )
{
if( fireDelay > 0 )
return false;
var weapon = Rules.WeaponInfo[ weaponName ];
var d = target.Location - self.Location;
if( weapon.Range * weapon.Range < d.X * d.X + d.Y * d.Y )
return false;
// FIXME: rules specifies ROF in 1/15 sec units; ticks are 1/25 sec
fireDelay = weapon.ROF;
game.world.Add( new Bullet( weaponName, self.Owner, self, self.CenterLocation.ToInt2(), target.CenterLocation.ToInt2(), game ) );
return true;
}
}
}

View File

@@ -35,7 +35,8 @@ namespace OpenRa.Game.Traits
static float2[] fvecs = Util.MakeArray<float2>(32,
i => -float2.FromAngle(i / 16.0f * (float)Math.PI) * new float2(1f, 1.3f));
int GetFacing(float2 d)
// TODO: move this somewhere more appropriate, now that AttackTurreted uses it.
public int GetFacing(float2 d)
{
if (float2.WithinEpsilon(d, float2.Zero, 0.001f))
return facing;

View File

@@ -5,18 +5,30 @@ using System.Text;
namespace OpenRa.Game.Traits
{
class Turreted : ITick // temporary.
class Turreted : ITick
{
public int turretFacing = Game.CellSize;
public int turretFacing = 0;
public int? desiredFacing;
public Turreted(Actor self)
{
}
// temporary.
public void Tick(Actor self, Game game, int dt)
{
turretFacing = (turretFacing + 1) % 32;
// TODO: desiredFacing should follow the base unit's facing only when not in combat.
// also, we want to be able to use this for GUN; avoid referencing Mobile.
var df = desiredFacing ?? self.traits.Get<Mobile>().facing;
if( turretFacing != desiredFacing )
{
var leftTurn = ( 32 + turretFacing - desiredFacing ) % 32;
var rightTurn = ( 32 + desiredFacing - turretFacing ) % 32;
if( leftTurn > rightTurn )
turretFacing = ( turretFacing + 1 ) % 32;
else
turretFacing = ( turretFacing + 31 ) % 32;
}
}
}
}