homing weapons more or less work now

This commit is contained in:
Chris Forbes
2009-12-06 21:19:02 +13:00
parent 5b970c499b
commit 3d457c397d
15 changed files with 211 additions and 61 deletions

View File

@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using OpenRa.Game.GameRules;
using OpenRa.Game.Graphics;
namespace OpenRa.Game.Effects
{
class Bullet : IEffect
{
readonly Player Owner;
readonly Actor FiredBy;
readonly WeaponInfo Weapon;
readonly ProjectileInfo Projectile;
readonly WarheadInfo Warhead;
readonly int2 Src;
readonly int2 Dest;
readonly int2 VisualDest;
int t = 0;
Animation anim;
const int BaseBulletSpeed = 100; /* pixels / 40ms frame */
/* src, dest are *pixel* coords */
public Bullet(string weapon, Player owner, Actor firedBy,
int2 src, int2 dest)
{
Owner = owner;
FiredBy = firedBy;
Src = src;
Dest = dest;
VisualDest = Dest + new int2(
Game.CosmeticRandom.Next(-10, 10),
Game.CosmeticRandom.Next(-10, 10));
Weapon = Rules.WeaponInfo[weapon];
Projectile = Rules.ProjectileInfo[Weapon.Projectile];
Warhead = Rules.WarheadInfo[Weapon.Warhead];
if (Projectile.Image != null && Projectile.Image != "none")
{
anim = new Animation(Projectile.Image);
if (Projectile.Rotates)
Traits.Util.PlayFacing(anim, "idle", () => Traits.Util.GetFacing((dest - src).ToFloat2(), 0));
else
anim.PlayRepeating("idle");
}
}
int TotalTime() { return (Dest - Src).Length * BaseBulletSpeed / Weapon.Speed; }
public void Tick()
{
if (t == 0)
Sound.Play(Weapon.Report + ".aud");
t += 40;
if (t > TotalTime()) /* remove finished bullets */
{
Game.world.AddFrameEndTask(w => w.Remove(this));
Combat.DoImpact(Dest, VisualDest, Weapon, Projectile, Warhead, FiredBy);
}
}
const float height = .1f;
public IEnumerable<Tuple<Sprite, float2, int>> Render()
{
if (anim != null)
{
var pos = float2.Lerp(
Src.ToFloat2(),
VisualDest.ToFloat2(),
(float)t / TotalTime()) - 0.5f * anim.Image.size;
if (Projectile.High || Projectile.Arcing)
{
if (Projectile.Shadow)
yield return Tuple.New(anim.Image, pos, 8);
var at = (float)t / TotalTime();
var highPos = pos - new float2(0, (VisualDest - Src).Length * height * 4 * at * (1 - at));
yield return Tuple.New(anim.Image, highPos, Owner.Palette);
}
else
yield return Tuple.New(anim.Image, pos, Owner.Palette);
}
}
}
}

View File

@@ -0,0 +1,29 @@
using System.Collections.Generic;
using OpenRa.Game.Graphics;
namespace OpenRa.Game.Effects
{
class Explosion : IEffect
{
Animation anim;
int2 pos;
public Explosion(int2 pixelPos, int style, bool isWater)
{
this.pos = pixelPos;
var variantSuffix = isWater ? "w" : "";
anim = new Animation("explosion");
anim.PlayThen(style.ToString() + variantSuffix,
() => Game.world.AddFrameEndTask(w => w.Remove(this)));
}
public void Tick() { anim.Tick(); }
public IEnumerable<Tuple<Sprite, float2, int>> Render()
{
yield return Tuple.New(anim.Image, pos.ToFloat2() - 0.5f * anim.Image.size, 0);
}
public Player Owner { get { return null; } }
}
}

View File

@@ -0,0 +1,11 @@
using System.Collections.Generic;
using OpenRa.Game.Graphics;
namespace OpenRa.Game.Effects
{
interface IEffect
{
void Tick();
IEnumerable<Tuple<Sprite, float2, int>> Render();
}
}

View File

@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRa.Game.Graphics;
using OpenRa.Game.GameRules;
namespace OpenRa.Game.Effects
{
class Missile : IEffect
{
readonly Player Owner;
readonly Actor FiredBy;
readonly WeaponInfo Weapon;
readonly ProjectileInfo Projectile;
readonly WarheadInfo Warhead;
float2 Pos;
readonly Actor Target;
readonly Animation anim;
int Facing;
int t;
public Missile(string weapon, Player owner, Actor firedBy,
int2 src, Actor target)
{
Weapon = Rules.WeaponInfo[weapon];
Projectile = Rules.ProjectileInfo[Weapon.Projectile];
Warhead = Rules.WarheadInfo[Weapon.Warhead];
FiredBy = firedBy;
Owner = owner;
Target = target;
Pos = src.ToFloat2();
/* todo: initial facing should be turret facing, or unit facing if we're not turreted */
Facing = Traits.Util.GetFacing( Target.CenterLocation - src.ToFloat2(), 0 );
if (Projectile.Image != null && Projectile.Image != "none")
{
anim = new Animation(Projectile.Image);
if (Projectile.Rotates)
Traits.Util.PlayFacing(anim, "idle", () => Facing);
else
anim.PlayRepeating("idle");
}
}
const int MissileCloseEnough = 7;
const float Scale = .3f;
public void Tick()
{
if (t == 0)
Sound.Play(Weapon.Report + ".aud");
t += 40;
Traits.Util.TickFacing(ref Facing,
Traits.Util.GetFacing(Target.CenterLocation - Pos, Facing),
Projectile.ROT);
anim.Tick();
var dist = Target.CenterLocation - Pos;
if (dist.LengthSquared < MissileCloseEnough * MissileCloseEnough || Target.IsDead)
{
Game.world.AddFrameEndTask(w => w.Remove(this));
if (t > Projectile.Arm * 40) /* don't blow up in our launcher's face! */
Combat.DoImpact(Pos.ToInt2(), Pos.ToInt2(), Weapon, Projectile, Warhead, FiredBy);
return;
}
var move = (Scale * Weapon.Speed / dist.Length) * dist;
Pos += move;
if (Projectile.Animates)
Game.world.AddFrameEndTask(w => w.Add(new Smoke((Pos - 1.5f * move).ToInt2())));
// todo: running out of fuel
// todo: turbo boost vs aircraft
}
public IEnumerable<Tuple<Sprite, float2, int>> Render()
{
yield return Tuple.New(anim.Image, Pos - 0.5f * anim.Image.size, 0);
}
}
}

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRa.Game.Graphics;
namespace OpenRa.Game.Effects
{
class Smoke : IEffect
{
readonly int2 pos;
readonly Animation anim = new Animation("smokey");
public Smoke(int2 pos)
{
this.pos = pos;
anim.PlayThen("idle",
() => Game.world.AddFrameEndTask(
w => w.Remove(this)));
}
public void Tick()
{
anim.Tick();
}
public IEnumerable<Tuple<Sprite, float2, int>> Render()
{
yield return Tuple.New(anim.Image, pos.ToFloat2() - .5f * anim.Image.size, 0);
}
}
}