Dear forum users! In compliance with the new European GDPR regulations, we'd just like to inform you that if you have an account, your email address is stored in our database. We do not share your information with third parties, and your email address and password are encrypted for security reasons.

New to the forum? Say hello in this topic! Also make sure to read the rules.

FacingDirection Not Working in This Script

Here you can find answered ScriptAPI topics.
Forum rules
By using the forum you agree to the following rules.
Post Reply
MrWheatley
Fighter
Fighter
Posts: 13
Joined: Wed Jun 06, 2018 7:36 am

FacingDirection Not Working in This Script

Post by MrWheatley » 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.

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;
	}
}
0 x

User avatar
JakSparro98
Superfighter
Superfighter
Posts: 530
Joined: Fri Jul 15, 2016 7:56 pm
Started SFD: PreAlpha 1.0.5
Location: Rome, Italy
Gender:
Age: 25

Post by JakSparro98 » Thu Jul 12, 2018 7:41 pm

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;
	}
}
1 x

Post Reply