Page 1 of 1

FacingDirection Not Working in This Script

Posted: Thu Jul 12, 2018 1:12 am
by MrWheatley
I was messing around with Motto73's Fake Death script to spawn objects instead of a body and it worked, but I want the object to have the same momentum as the player. It only worked in one direction though.

Code: Select all

public void OnStartup ()
{
	Events.UpdateCallback.Start (OnUpdate);
	foreach (IPlayer p in Game.GetPlayers ())
		Thing._Things.Add (new Thing (p));
}
public void OnUpdate (float e)
{
	foreach (Thing t in Thing._Things)
	{
		if (t.Corpse == null || t.Corpse.IsRemoved)
		{
			if (t.Controller != null && !t.Controller.IsRemoved)
				t.Controller.Remove ();
			Thing._Things.Remove (t);
			return;
		}
		if (t.Controller != null && !t.Controller.IsRemoved && t.Corpse != null && !t.Corpse.IsRemoved)
		{
			t.Controller.SetHealth (t.Controller.GetHealth () - (t.Corpse.Statistics.TotalDamageTaken - t.LastHealth));
			t.LastHealth = t.Corpse.Statistics.TotalDamageTaken;
			if (t.Controller.GetHealth () <= 1)
			{
				t.Controller.Remove ();
				t.Controller = null;
				continue;
			}
			if (t.Controller.IsWalking)
			{
				IObject urmom3 = (IObject) Game.GetSingleObjectByCustomID ("urmom2");
				var ply = Game.CreatePlayer (urmom3.GetWorldPosition ());
				ply.SetProfile (t.Corpse.GetProfile ());
				ply.SetModifiers (t.Corpse.GetModifiers ());
				ply.CustomId = t.Corpse.CustomId;
				ply.SetHealth (t.Controller.GetHealth ());
				ply.SetTeam (t.Controller.GetTeam ());
				ply.SetNametagVisible (t.Corpse.GetNametagVisible ());
				ply.SetStatusBarsVisible (t.Corpse.GetStatusBarsVisible ());
				t.Corpse.Remove ();
				t.Corpse = ply;
				t.Corpse.SetUser (t.User);
				t.Controller.Remove ();
				t.Controller = null;
				urmom3.Remove ();
				t.Corpse.SetNametagVisible (true);
				continue;
			}
			else if (t.Controller.GetUser () != t.User)
				t.Controller.SetUser (t.User);
		}
		else
		{
			if (t.Corpse.IsWalking)
			{
				Game.CreateObject ("InvisibleBlock", Game.GetCameraMaxArea ().TopLeft).SetSizeFactor (
					new Point ((int) (Math.Abs (Game.GetCameraMaxArea ().Left) + Math.Abs (Game.GetCameraMaxArea ().Right)), 1));
				t.Controller = Game.CreatePlayer (new Vector2 (t.Corpse.GetWorldPosition ().X, Game.GetCameraMaxArea ().Top + 8));
				IObject urmom = Game.CreateObject ("BarrelExplosive", t.Corpse.GetWorldPosition ());
				urmom.CustomId = "urmom2";
				t.Controller.SetUser (t.User);
				t.Controller.SetNametagVisible (false);
				t.Controller.SetTeam (t.Corpse.GetTeam ());
				t.Controller.SetHealth (t.Corpse.GetHealth ());
				t.Corpse.SetNametagVisible (false);
				t.Corpse.SetWorldPosition (new Vector2 (t.Corpse.GetWorldPosition ().X, Game.GetCameraMaxArea ().Top + 8));
				Game.PlayEffect ("FIRE", (urmom.GetWorldPosition ()));

				if (t.Controller.FacingDirection == -1)
				{
					urmom.SetLinearVelocity (t.Corpse.GetLinearVelocity () + new Vector2 (-11.5f, 0));
				}

				if (t.Controller.FacingDirection == 1)
				{
					urmom.SetLinearVelocity (t.Corpse.GetLinearVelocity () + new Vector2 (11.5f, 0));
				}

				t.LastHealth = t.Corpse.Statistics.TotalDamageTaken;
			}
		}
		if (t.Corpse == null || t.Corpse.IsRemoved)
		{
			if (t.Controller != null && !t.Controller.IsRemoved)
				t.Controller.Remove ();
			Thing._Things.Remove (t);
			return;
		}
		if ((t.Controller == null || t.Controller.IsRemoved || t.Controller.IsDead) && t.Corpse.IsDead)
		{
			if (t.Controller != null && !t.Controller.IsRemoved)
				t.Controller.Remove ();
			Thing._Things.Remove (t);
			return;
		}
	}
}
class Thing
{
	public static List<Thing> _Things = new List<Thing> ();
	public IPlayer Corpse;
	public float LastHealth;
	public IPlayer Controller;
	public IUser User;

	public Thing (IPlayer p)
	{
		User = p.GetUser ();
		Corpse = p;
	}
}

Re: FacingDirection Not Working in This Script

Posted: Thu Jul 12, 2018 7:41 pm
by JakSparro98
MrWheatley wrote:
Thu Jul 12, 2018 1:12 am
I was messing around with Motto73's Fake Death script to spawn objects instead of a body and it worked, but I want the object to have the same momentum as the player. It only worked in one direction though.
It only works on one direction because the FacingDirection condition was listening on the wrong player (t.Controller instead of t.Corpse), furthermore the momentum was set to a fixed value plus the linear velocity of the player, I only left this latter to get the same player velocity vector.

Code: Select all

public void OnStartup ()
{
	Events.UpdateCallback.Start (OnUpdate);
	foreach (IPlayer p in Game.GetPlayers ())
		Thing._Things.Add (new Thing (p));
}
public void OnUpdate (float e)
{
	foreach (Thing t in Thing._Things)
	{
		if (t.Corpse == null || t.Corpse.IsRemoved)
		{
			if (t.Controller != null && !t.Controller.IsRemoved)
				t.Controller.Remove ();
			Thing._Things.Remove (t);
			return;
		}
		if (t.Controller != null && !t.Controller.IsRemoved && t.Corpse != null && !t.Corpse.IsRemoved)
		{
			t.Controller.SetHealth (t.Controller.GetHealth () - (t.Corpse.Statistics.TotalDamageTaken - t.LastHealth));
			t.LastHealth = t.Corpse.Statistics.TotalDamageTaken;
			if (t.Controller.GetHealth () <= 1)
			{
				t.Controller.Remove ();
				t.Controller = null;
				continue;
			}
			if (t.Controller.IsWalking)
			{
				IObject urmom3 = (IObject) Game.GetSingleObjectByCustomID ("urmom2");
				var ply = Game.CreatePlayer (urmom3.GetWorldPosition ());
				ply.SetProfile (t.Corpse.GetProfile ());
				ply.SetModifiers (t.Corpse.GetModifiers ());
				ply.CustomId = t.Corpse.CustomId;
				ply.SetHealth (t.Controller.GetHealth ());
				ply.SetTeam (t.Controller.GetTeam ());
				ply.SetNametagVisible (t.Corpse.GetNametagVisible ());
				ply.SetStatusBarsVisible (t.Corpse.GetStatusBarsVisible ());
				t.Corpse.Remove ();
				t.Corpse = ply;
				t.Corpse.SetUser (t.User);
				t.Controller.Remove ();
				t.Controller = null;
				urmom3.Remove ();
				t.Corpse.SetNametagVisible (true);
				continue;
			}
			else if (t.Controller.GetUser () != t.User)
				t.Controller.SetUser (t.User);
		}
		else
		{
			if (t.Corpse.IsWalking)
			{
				Game.CreateObject ("InvisibleBlock", Game.GetCameraMaxArea ().TopLeft).SetSizeFactor (
					new Point ((int) (Math.Abs (Game.GetCameraMaxArea ().Left) + Math.Abs (Game.GetCameraMaxArea ().Right)), 1));
				t.Controller = Game.CreatePlayer (new Vector2 (t.Corpse.GetWorldPosition ().X, Game.GetCameraMaxArea ().Top + 8));
				IObject urmom = Game.CreateObject ("BarrelExplosive", t.Corpse.GetWorldPosition ());
				urmom.CustomId = "urmom2";
				t.Controller.SetUser (t.User);
				t.Controller.SetNametagVisible (false);
				t.Controller.SetTeam (t.Corpse.GetTeam ());
				t.Controller.SetHealth (t.Corpse.GetHealth ());
				t.Corpse.SetNametagVisible (false);
				t.Corpse.SetWorldPosition (new Vector2 (t.Corpse.GetWorldPosition ().X, Game.GetCameraMaxArea ().Top + 8));
				Game.PlayEffect ("FIRE", (urmom.GetWorldPosition ()));


					urmom.SetLinearVelocity (t.Corpse.GetLinearVelocity ());


				t.LastHealth = t.Corpse.Statistics.TotalDamageTaken;
			}
		}
		if (t.Corpse == null || t.Corpse.IsRemoved)
		{
			if (t.Controller != null && !t.Controller.IsRemoved)
				t.Controller.Remove ();
			Thing._Things.Remove (t);
			return;
		}
		if ((t.Controller == null || t.Controller.IsRemoved || t.Controller.IsDead) && t.Corpse.IsDead)
		{
			if (t.Controller != null && !t.Controller.IsRemoved)
				t.Controller.Remove ();
			Thing._Things.Remove (t);
			return;
		}
	}
}
class Thing
{
	public static List<Thing> _Things = new List<Thing> ();
	public IPlayer Corpse;
	public float LastHealth;
	public IPlayer Controller;
	public IUser User;

	public Thing (IPlayer p)
	{
		User = p.GetUser ();
		Corpse = p;
	}
}