Optimised code
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class IQEnemy : RigidBody2D
|
||||
public partial class IQEnemy : CharacterBody2D
|
||||
{
|
||||
// Misc
|
||||
float GlobalTimer;
|
||||
@@ -12,11 +12,14 @@ public partial class IQEnemy : RigidBody2D
|
||||
public float MaxHP;
|
||||
float RegenerationAmmount;
|
||||
float RegenerationInterval;
|
||||
float WalkSpeed;
|
||||
float JumpHeight;
|
||||
|
||||
// Enemy Conditions
|
||||
float CurrentYVelocity;
|
||||
float PreviousYVelocity;
|
||||
public bool IsGrounded;
|
||||
bool GroundDetected;
|
||||
bool LedgeDetected;
|
||||
|
||||
// Enemy Abilities
|
||||
bool AbilityJump = false;
|
||||
@@ -41,6 +44,10 @@ public partial class IQEnemy : RigidBody2D
|
||||
else RegenerationAmmount = 1.0f;
|
||||
if (HasMeta("RegenerationInterval")) RegenerationInterval = (float)GetMeta("RegenerationInterval");
|
||||
else RegenerationInterval = 1.0f;
|
||||
if (HasMeta("WalkSpeed")) WalkSpeed = (float)GetMeta("WalkSpeed");
|
||||
else WalkSpeed = 10.0f;
|
||||
if (HasMeta("JumpHeight")) JumpHeight = (float)GetMeta("JumpHeight");
|
||||
else JumpHeight = 10.0f;
|
||||
|
||||
// Read and set stats
|
||||
if (HasMeta("MaxHP")) MaxHP = (float)GetMeta("MaxHP");
|
||||
@@ -49,7 +56,11 @@ public partial class IQEnemy : RigidBody2D
|
||||
GetNode<Area2D>("GroundDetector").AreaEntered += OnGroundEntered;
|
||||
GetNode<Area2D>("GroundDetector").AreaExited += OnGroundExited;
|
||||
|
||||
IsGrounded = true;
|
||||
GetNode<Area2D>("LedgeDetector").AreaEntered += OnLedgeEntered;
|
||||
GetNode<Area2D>("LedgeDetector").AreaExited += OnLedgeExited;
|
||||
|
||||
GroundDetected = true;
|
||||
LedgeDetected = true;
|
||||
|
||||
// Apply IQ based permenant modifiers
|
||||
IQ = (int)GetMeta("IQ"); // Read IQ
|
||||
@@ -73,9 +84,19 @@ public partial class IQEnemy : RigidBody2D
|
||||
if (GlobalTimer % RegenerationInterval == 0){Regenerate(RegenerationAmmount);}
|
||||
}
|
||||
|
||||
// Movement
|
||||
// Add gravity.
|
||||
if (GroundDetected == false){Velocity = new Vector2(Velocity[0],-1);}
|
||||
if (IQ <= 20){ // Goomba ahh diddy blud
|
||||
if (GroundDetected && LedgeDetected == false) {Flip("ANY");}
|
||||
if (Scale[0] > 0){Velocity = new Vector2(WalkSpeed, Velocity[1]);} // Facing right
|
||||
if (Scale[0] < 0){Velocity = new Vector2(-1 * WalkSpeed, Velocity[1]);} // Facing left
|
||||
}
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
||||
void Flip(string facing){
|
||||
if (facing == "ANY") Scale = new Vector2(Scale[0] * -1,1);
|
||||
if (facing == "RIGHT") Scale = new Vector2(1,1);
|
||||
if (facing =="LEFT") Scale = new Vector2(-1,1);
|
||||
}
|
||||
@@ -95,6 +116,9 @@ public partial class IQEnemy : RigidBody2D
|
||||
}
|
||||
|
||||
// Ground Detection
|
||||
public void OnGroundEntered(Area2D area){if (area.CollisionLayer == 0) IsGrounded = true;}
|
||||
public void OnGroundExited(Area2D area){if (area.CollisionLayer == 0) IsGrounded = false;}
|
||||
public void OnGroundEntered(Area2D area){if (area.CollisionLayer == 0) GroundDetected = true;}
|
||||
public void OnGroundExited(Area2D area){if (area.CollisionLayer == 0) GroundDetected = false;}
|
||||
// Ledge Detection
|
||||
public void OnLedgeEntered(Area2D area){if (area.CollisionLayer == 0) LedgeDetected = true;}
|
||||
public void OnLedgeExited(Area2D area){if (area.CollisionLayer == 0) LedgeDetected = false;}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user