Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
Fly mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ForLoveOfCats committed Feb 15, 2019
1 parent e2912fc commit 9fffb5f
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 29 deletions.
176 changes: 147 additions & 29 deletions Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ public class Player : KinematicBody
private const float LookDivisor = 6;

private bool Frozen = true;
public bool FlyMode { get; private set;} = false;

public System.Tuple<int, int> CurrentChunk = new System.Tuple<int, int>(0, 0);

private int ForwardAxis = 0;
private int RightAxis = 0;
private int JumpAxis = 0;
public bool IsCrouching = false;
public bool IsSprinting = false;
public bool IsJumping = false;
public bool WasOnFloor = false;
Expand Down Expand Up @@ -111,6 +113,17 @@ public void SetFreeze(bool NewFrozen)
}


public void SetFly(bool NewFly) //because custom setters are weird
{
FlyMode = NewFly;

if(!IsOnFloor())
{
OnAir();
}
}


public void PositionReset()
{
Translation = new Vector3(0,1,0);
Expand Down Expand Up @@ -216,7 +229,7 @@ public void ForwardMove(double Sens)
BackwardSens = 0d;
ForwardAxis = 1;

if(IsOnFloor() && JumpAxis < 1)
if((IsOnFloor() && JumpAxis < 1) || FlyMode)
{
if(IsSprinting)
{
Expand Down Expand Up @@ -246,7 +259,7 @@ public void BackwardMove(double Sens)
ForwardSens = 0d;
ForwardAxis = -1;

if(IsOnFloor() && JumpAxis < 1)
if((IsOnFloor() && JumpAxis < 1) || FlyMode)
{
if(IsSprinting)
{
Expand Down Expand Up @@ -276,7 +289,7 @@ public void RightMove(double Sens)
LeftSens = 0d;
RightAxis = 1;

if(IsOnFloor() && JumpAxis < 1)
if((IsOnFloor() && JumpAxis < 1) || FlyMode)
{
if(IsSprinting)
{
Expand Down Expand Up @@ -306,7 +319,7 @@ public void LeftMove(double Sens)
RightSens = 0d;
RightAxis = -1;

if(IsOnFloor() && JumpAxis < 1)
if((IsOnFloor() && JumpAxis < 1) || FlyMode)
{
if(IsSprinting)
{
Expand All @@ -331,7 +344,7 @@ public void Sprint(double Sens)
SprintSens = Sens;
if(Sens > 0d)
{
if(IsOnFloor())
if(IsOnFloor() || FlyMode)
{
IsSprinting = true;

Expand All @@ -344,16 +357,26 @@ public void Sprint(double Sens)
{
Momentum.x = Momentum.x*SprintMultiplyer;
}

if(FlyMode)
{
Momentum.y = Momentum.y*SprintMultiplyer;
}
}
}
else
{
if(IsOnFloor())
if(IsOnFloor() || FlyMode)
{
IsSprinting = false;

Momentum.z = Mathf.Clamp(Momentum.z, -BaseMovementSpeed, BaseMovementSpeed);
Momentum.x = Mathf.Clamp(Momentum.x, -BaseMovementSpeed, BaseMovementSpeed);

if(FlyMode)
{
Momentum.y = Mathf.Clamp(Momentum.y, -BaseMovementSpeed, BaseMovementSpeed);
}
}
}
}
Expand All @@ -365,7 +388,21 @@ public void Jump(double Sens)
if(Sens > 0d)
{
JumpAxis = 1;
if(IsOnFloor() && ShouldDo.LocalPlayerJump())
IsCrouching = false;

if(FlyMode && ShouldDo.LocalPlayerJump())
{
if(IsSprinting)
{
Momentum.y = BaseMovementSpeed*SprintMultiplyer;
}
else
{
Momentum.y = BaseMovementSpeed;
}
IsJumping = false;
}
else if(IsOnFloor() && ShouldDo.LocalPlayerJump())
{
Momentum.y = JumpStartForce;
IsJumping = true;
Expand All @@ -379,6 +416,33 @@ public void Jump(double Sens)
}


public void Crouch(double Sens)
{
if(Sens > 0)
{
IsCrouching = true;
JumpAxis = 0;
JumpSens = 0;

if(FlyMode)
{
if(IsSprinting)
{
Momentum.y = -BaseMovementSpeed*SprintMultiplyer;
}
else
{
Momentum.y = -BaseMovementSpeed;
}
}
}
else
{
IsCrouching = false;
}
}


public void LookUp(double Sens)
{
if(Sens > 0d)
Expand Down Expand Up @@ -493,7 +557,7 @@ public void SecondaryFire(double Sens)
}


private void OnFly()
private void OnAir()
{
Momentum = Momentum.Rotated(new Vector3(0,1,0), Deg2Rad(LookHorizontal));
}
Expand Down Expand Up @@ -584,18 +648,21 @@ public override void _PhysicsProcess(float Delta)
}


if(IsOnFloor())
if(!FlyMode)
{
if(!WasOnFloor)
if(IsOnFloor())
{
OnLand();
if(!WasOnFloor)
{
OnLand();
}
}
else if(WasOnFloor)
{
OnAir();
}
WasOnFloor = IsOnFloor();
}
else if(WasOnFloor)
{
OnFly();
}
WasOnFloor = IsOnFloor();

if(JumpAxis < 1)
{
Expand Down Expand Up @@ -623,32 +690,83 @@ public override void _PhysicsProcess(float Delta)
}
}
}
else
else if(!FlyMode)
{
Jump(JumpSens);
}

if(IsJumping && JumpTimer <= MaxJumpLength)
{
JumpTimer += Delta;
Momentum.y = Mathf.Clamp(Momentum.y+JumpContinueForce*Delta, -MaxMovementSpeed, MaxMovementSpeed);
}
else
if(FlyMode)
{
JumpTimer = 0f;
IsJumping = false;
Momentum.y = Mathf.Clamp(Momentum.y-Gravity*Delta, -MaxMovementSpeed, MaxMovementSpeed);
if(ForwardAxis == 0)
{
if(Momentum.z > 0)
{
Momentum.z = Mathf.Clamp(Momentum.z-Friction*Delta, 0f, MaxMovementSpeed);
}
else if (Momentum.z < 0)
{
Momentum.z = Mathf.Clamp(Momentum.z+Friction*Delta, -MaxMovementSpeed, 0f);
}
}

if(RightAxis == 0)
{
if(Momentum.x > 0)
{
Momentum.x = Mathf.Clamp(Momentum.x-Friction*Delta, 0f, MaxMovementSpeed);
}
else if (Momentum.x < 0)
{
Momentum.x = Mathf.Clamp(Momentum.x+Friction*Delta, -MaxMovementSpeed, 0f);
}
}

if(JumpAxis < 1 && !IsCrouching)
{
if(Momentum.y > 0)
{
Momentum.y = Mathf.Clamp(Momentum.y-Friction*Delta, 0f, MaxMovementSpeed);
}
if(Momentum.y < 0)
{
Momentum.y = Mathf.Clamp(Momentum.y+Friction*Delta, 0f, MaxMovementSpeed);
}
}
}

if(!IsOnFloor())
if(!FlyMode)
{
Momentum = AirAccelerate(Momentum, new Vector3(-RightAxis*MovementInputMultiplyer, 0, ForwardAxis*MovementInputMultiplyer).Rotated(new Vector3(0,1,0), Deg2Rad(LookHorizontal)), Delta);
if(IsJumping && JumpTimer <= MaxJumpLength)
{
JumpTimer += Delta;
Momentum.y = Mathf.Clamp(Momentum.y+JumpContinueForce*Delta, -MaxMovementSpeed, MaxMovementSpeed);
}
else
{
JumpTimer = 0f;
IsJumping = false;
Momentum.y = Mathf.Clamp(Momentum.y-Gravity*Delta, -MaxMovementSpeed, MaxMovementSpeed);
}

if(!IsOnFloor())
{
Momentum = AirAccelerate(Momentum, new Vector3(-RightAxis*MovementInputMultiplyer, 0, ForwardAxis*MovementInputMultiplyer).Rotated(new Vector3(0,1,0), Deg2Rad(LookHorizontal)), Delta);
}
}

Vector3 OldPos = Translation;
//100 bounces in order to allow players to go up slopes more quickly
//MoveAndSlide multiplies by *physics* delta internally
if(IsOnFloor())
if(FlyMode)
{
Vector3 FlatVel = Momentum;
FlatVel.y = 0;
MoveAndSlide(FlatVel.Rotated(new Vector3(1,0,0), Mathf.Deg2Rad(LoopRotation(-LookVertical)))
.Rotated(new Vector3(0,1,0), Mathf.Deg2Rad(LookHorizontal)), new Vector3(0,1,0), true, 100, Mathf.Deg2Rad(60));

MoveAndSlide(new Vector3(0,Momentum.y,0).Rotated(new Vector3(0,1,0), Mathf.Deg2Rad(LookHorizontal)), new Vector3(0,1,0), true, 100, Mathf.Deg2Rad(60));
}
else if(IsOnFloor())
{
MoveAndSlide(Momentum.Rotated(new Vector3(0,1,0), Mathf.Deg2Rad(LookHorizontal)), new Vector3(0,1,0), true, 100, Mathf.Deg2Rad(60));
}
Expand Down
28 changes: 28 additions & 0 deletions Scripting/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ static List<object> GetDelCall(string Name)
case "player_input_jump_get":
return new List<object> {Name, new Func<double>(() => {return Game.PossessedPlayer.IsJumping ? 1d : 0d;})};

case "player_input_crouch":
return new List<object> {Name, new Action<double>(delegate(double Sens){
Game.PossessedPlayer.Crouch(Sens);
})};

case "player_input_crouch_get":
return new List<object> {Name, new Func<double>(() => {return Game.PossessedPlayer.IsCrouching ? 1d : 0d;})};

case "player_input_inventory_up":
return new List<object> {Name, new Action(delegate(){
Game.PossessedPlayer.InventoryUp();
Expand Down Expand Up @@ -164,6 +172,14 @@ static List<object> GetDelCall(string Name)
Game.PossessedPlayer.PositionReset();
})};

case "fly":
return new List<object> {Name, new Action<bool>(delegate(bool NewFly){
Game.PossessedPlayer.SetFly(NewFly);
})};

case "fly_get":
return new List<object> {Name, new Func<bool>(() => {return Game.PossessedPlayer.FlyMode;})};

case "gamemode":
return new List<object> {Name, new Action<string>(delegate(string GameModeName){
if(Game.Self.GetTree().GetNetworkPeer() != null && Game.Self.GetTree().GetNetworkUniqueId() == 1)
Expand Down Expand Up @@ -307,6 +323,8 @@ public static List<List<object>> Expose(LEVEL ApiLevel, Scripting ScriptingRef)
Output.Add(GetDelCall("player_input_sprint_get"));
Output.Add(GetDelCall("player_input_jump"));
Output.Add(GetDelCall("player_input_jump_get"));
Output.Add(GetDelCall("player_input_crouch"));
Output.Add(GetDelCall("player_input_crouch_get"));
Output.Add(GetDelCall("player_input_inventory_up"));
Output.Add(GetDelCall("player_input_inventory_down"));
Output.Add(GetDelCall("player_input_look_up"));
Expand All @@ -329,6 +347,8 @@ public static List<List<object>> Expose(LEVEL ApiLevel, Scripting ScriptingRef)
Output.Add(GetDelCall("load"));
Output.Add(GetDelCall("hud_hide"));
Output.Add(GetDelCall("hud_show"));
Output.Add(GetDelCall("fly"));
Output.Add(GetDelCall("fly_get"));
break;
case LEVEL.SERVER_GM:
Output.Add(GetDelCall("log"));
Expand All @@ -346,6 +366,8 @@ public static List<List<object>> Expose(LEVEL ApiLevel, Scripting ScriptingRef)
Output.Add(GetDelCall("player_input_sprint_get"));
Output.Add(GetDelCall("player_input_jump"));
Output.Add(GetDelCall("player_input_jump_get"));
Output.Add(GetDelCall("player_input_crouch"));
Output.Add(GetDelCall("player_input_crouch_get"));
Output.Add(GetDelCall("player_input_inventory_up"));
Output.Add(GetDelCall("player_input_inventory_down"));
Output.Add(GetDelCall("player_input_look_up"));
Expand All @@ -365,6 +387,8 @@ public static List<List<object>> Expose(LEVEL ApiLevel, Scripting ScriptingRef)
Output.Add(GetDelCall("load"));
Output.Add(GetDelCall("hud_hide"));
Output.Add(GetDelCall("hud_show"));
Output.Add(GetDelCall("fly"));
Output.Add(GetDelCall("fly_get"));
break;
case LEVEL.CLIENT_GM:
Output.Add(GetDelCall("log"));
Expand All @@ -382,6 +406,8 @@ public static List<List<object>> Expose(LEVEL ApiLevel, Scripting ScriptingRef)
Output.Add(GetDelCall("player_input_sprint_get"));
Output.Add(GetDelCall("player_input_jump"));
Output.Add(GetDelCall("player_input_jump_get"));
Output.Add(GetDelCall("player_input_crouch"));
Output.Add(GetDelCall("player_input_crouch_get"));
Output.Add(GetDelCall("player_input_inventory_up"));
Output.Add(GetDelCall("player_input_inventory_down"));
Output.Add(GetDelCall("player_input_look_up"));
Expand All @@ -399,6 +425,8 @@ public static List<List<object>> Expose(LEVEL ApiLevel, Scripting ScriptingRef)
Output.Add(GetDelCall("fps_max_get"));
Output.Add(GetDelCall("hud_hide"));
Output.Add(GetDelCall("hud_show"));
Output.Add(GetDelCall("fly"));
Output.Add(GetDelCall("fly_get"));
break;
}

Expand Down
1 change: 1 addition & 0 deletions Scripting/SetupScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ bind("player_input_right", "D");
bind("player_input_left", "A");
bind("player_input_sprint", "Shift");
bind("player_input_jump", "Space");
bind("player_input_crouch", "Control");

bind("player_input_inventory_up", "WheelUp");
bind("player_input_inventory_down", "WheelDown");
Expand Down

0 comments on commit 9fffb5f

Please sign in to comment.