fixing bullet offset

This commit is contained in:
Chris Forbes
2009-10-19 21:59:31 +13:00
parent 670df0f7e1
commit 451fdc1615

View File

@@ -1,63 +1,64 @@
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 IjwFramework.Types; using IjwFramework.Types;
using OpenRa.Game.Graphics; using OpenRa.Game.Graphics;
namespace OpenRa.Game namespace OpenRa.Game
{ {
class Bullet class Bullet
{ {
public readonly Player Owner; public readonly Player Owner;
readonly Actor FiredBy; readonly Actor FiredBy;
readonly WeaponInfo Weapon; readonly WeaponInfo Weapon;
readonly ProjectileInfo Projectile; readonly ProjectileInfo Projectile;
readonly WarheadInfo Warhead; readonly WarheadInfo Warhead;
readonly int2 Src; readonly int2 Src;
readonly int2 Dest; readonly int2 Dest;
int t = 0; int t = 0;
Animation anim; Animation anim;
const int BaseBulletSpeed = 100; /* pixels / 40ms frame */ const int BaseBulletSpeed = 100; /* pixels / 40ms frame */
/* src, dest are *pixel* coords */ /* src, dest are *pixel* coords */
public Bullet(string weapon, Player owner, Actor firedBy, public Bullet(string weapon, Player owner, Actor firedBy,
int2 src, int2 dest, Game game) int2 src, int2 dest, Game game)
{ {
Owner = owner; Owner = owner;
FiredBy = firedBy; FiredBy = firedBy;
Src = src; Src = src;
Dest = dest; Dest = dest;
Weapon = Rules.WeaponInfo[weapon]; Weapon = Rules.WeaponInfo[weapon];
Projectile = Rules.ProjectileInfo[Weapon.Projectile]; Projectile = Rules.ProjectileInfo[Weapon.Projectile];
Warhead = Rules.WarheadInfo[Weapon.Warhead]; Warhead = Rules.WarheadInfo[Weapon.Warhead];
anim = new Animation(Projectile.Image); anim = new Animation(Projectile.Image);
anim.PlayRepeating("idle"); anim.PlayRepeating("idle");
} }
int TotalTime() { return (Dest - Src).Length * BaseBulletSpeed / Weapon.Speed; } int TotalTime() { return (Dest - Src).Length * BaseBulletSpeed / Weapon.Speed; }
public void Tick(Game game, int dt) public void Tick(Game game, int dt)
{ {
if (t == 0) if (t == 0)
game.PlaySound(Weapon.Report + ".aud", false); game.PlaySound(Weapon.Report + ".aud", false);
t += dt; t += dt;
//if (t > TotalTime())
// t = 0; /* temporary! loop the bullet forever */ if (t > TotalTime()) /* remove finished bullets */
} game.world.AddFrameEndTask(w => w.Remove(this));
}
public IEnumerable<Pair<Sprite, float2>> Render()
{ public IEnumerable<Pair<Sprite, float2>> Render()
yield return Pair.New(anim.Image, {
float2.Lerp( yield return Pair.New(anim.Image,
Src.ToFloat2(), float2.Lerp(
Dest.ToFloat2(), Src.ToFloat2(),
(float)t / TotalTime())); Dest.ToFloat2(),
} (float)t / TotalTime()) - 0.5f * anim.Image.size);
} }
} }
}