From 46d52d077cd58791ccd67929a3fda09e76e12769 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Sun, 18 Oct 2009 22:56:34 +1300 Subject: [PATCH] bullet test --- OpenRa.DataStructures/int2.cs | 1 + OpenRa.Game/Bullet.cs | 32 +++++++++++++++++++++++++++++--- OpenRa.Game/MainWindow.cs | 5 ++++- sequences.xml | 4 ++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/OpenRa.DataStructures/int2.cs b/OpenRa.DataStructures/int2.cs index 857c421e35..cedec2e842 100644 --- a/OpenRa.DataStructures/int2.cs +++ b/OpenRa.DataStructures/int2.cs @@ -23,6 +23,7 @@ namespace OpenRa public int2 Sign() { return new int2(Math.Sign(X), Math.Sign(Y)); } public int2 Abs() { return new int2( Math.Abs( X ), Math.Abs( Y ) ); } + public int Length { get { return (int)Math.Sqrt(X * X + Y * Y); } } public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } public override bool Equals(object obj) diff --git a/OpenRa.Game/Bullet.cs b/OpenRa.Game/Bullet.cs index 781be45cb7..6a9b90f61e 100644 --- a/OpenRa.Game/Bullet.cs +++ b/OpenRa.Game/Bullet.cs @@ -18,9 +18,14 @@ namespace OpenRa.Game readonly int2 Src; readonly int2 Dest; + int t = 0; + Animation anim; + + const int BaseBulletSpeed = 100; /* pixels / ms */ + /* src, dest are *pixel* coords */ public Bullet(string weapon, Player owner, Actor firedBy, - int2 src, int2 dest) + int2 src, int2 dest, Game game) { Owner = owner; FiredBy = firedBy; @@ -29,9 +34,30 @@ namespace OpenRa.Game Weapon = Rules.WeaponInfo[weapon]; Projectile = Rules.ProjectileInfo[Weapon.Projectile]; Warhead = Rules.WarheadInfo[Weapon.Warhead]; + + anim = new Animation(Projectile.Image); + anim.PlayRepeating("idle"); } - public void Tick(Game game, int dt) { /* todo */ } - public IEnumerable> Render() { yield break; } + int TotalTime() { return (Dest - Src).Length * BaseBulletSpeed / Weapon.Speed; } + + public void Tick(Game game, int dt) + { + if (t == 0) + game.PlaySound(Weapon.Report + ".aud", false); + + t += dt; + if (t > TotalTime()) + t = 0; /* temporary! loop the bullet forever */ + } + + public IEnumerable> Render() + { + yield return Pair.New(anim.Image, + float2.Lerp( + Src.ToFloat2(), + Dest.ToFloat2(), + (float)t / TotalTime())); + } } } diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 0e18e5e419..734619431d 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -58,7 +58,10 @@ using System.Runtime.InteropServices; game.world.Add( new Actor( "mcv", new int2( 5, 5 ), game.players[ 3 ]) ); game.world.Add( new Actor( "mcv", new int2( 7, 5 ), game.players[ 2 ] ) ); game.world.Add( new Actor( "mcv", new int2( 9, 5 ), game.players[ 0 ] ) ); - game.world.Add( new Actor( "jeep", new int2( 9, 7 ), game.players[ 1 ] ) ); + game.world.Add( new Actor( "jeep", new int2( 9, 7 ), game.players[ 1 ] ) ); + + game.world.Add(new Bullet("105mm", game.players[1], null, + new int2(200, 200), new int2(400, 200), game)); sidebar = new Sidebar(renderer, game); diff --git a/sequences.xml b/sequences.xml index 32bf1239f4..d40748c56f 100644 --- a/sequences.xml +++ b/sequences.xml @@ -330,5 +330,9 @@ + + + +