cleaned up scripts a little bit

This commit is contained in:
slevy14
2023-04-17 16:47:12 -07:00
parent 275763628b
commit cb059613f9
7 changed files with 170 additions and 346 deletions

View File

@@ -7,18 +7,11 @@ public class PlayerBehavior : MonoBehaviour
{
[Header("Physics:")]
public float moveSpeed;
public float jumpSpeed;
public float airSpeed;
private float _hInput;
private Rigidbody2D _rb;
private int forward = 1;
public PlayerInput playerInput;
public LayerMask groundLayer;
public Vector2 boxSize;
public float maxDistanceFromGround;
[Header("Tambourine:")]
[SerializeField] private Launch launcher;
[HideInInspector] public bool hasTambourine = true;
@@ -37,7 +30,6 @@ public class PlayerBehavior : MonoBehaviour
void Start()
{
_rb = GetComponent<Rigidbody2D>();
airSpeed = .5f * moveSpeed;
stateController = GameObject.Find("StateController").GetComponent<StateController>();
}
@@ -112,4 +104,25 @@ public class PlayerBehavior : MonoBehaviour
}
grapplingGun.ReleaseGrapple();
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
grappleSurface = col.gameObject;
}
else if (col.tag == "instaDeath")
{
this.stateController.SetDeathCanvasActive(true);
Destroy(this.gameObject);
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
grappleSurface = null;
}
}
}

View File

@@ -23,18 +23,11 @@ public class PlayerMovement : MonoBehaviour
public Vector2 boxSize;
public float maxDistanceFromGround;
[Header("Grappling:")]
[SerializeField] public Tutorial_GrapplingGun grapplingGun;
[SerializeField] public Tutorial_GrapplingRope grapplingRope;
private GameObject grappleSurface;
[Header("Tambourine:")]
[SerializeField] private Launch launcher;
[HideInInspector] public bool hasTambourine = true;
[Header("State Control:")]
[SerializeField] private StateController stateController;
PlayerBehavior playerBehavior;
void OnValidate()
{
this.runAcceleration = Mathf.Clamp(runAcceleration, 0.1f, this.maxRunSpeed);
@@ -42,6 +35,7 @@ public class PlayerMovement : MonoBehaviour
void Start()
{
playerBehavior = this.gameObject.GetComponent<PlayerBehavior>();
this.rb = this.GetComponent<Rigidbody2D>();
stateController = GameObject.Find("StateController").GetComponent<StateController>();
}
@@ -88,14 +82,16 @@ public class PlayerMovement : MonoBehaviour
float frictionAmount = 0.5f;
// accelerate
if (onGround && (Mathf.Abs(this.movement.x) > 0.1f)) {
if (onGround && (Mathf.Abs(this.movement.x) > 0.1f)) { // regular acceleration
this.rb.AddForce(move * Vector2.right, ForceMode2D.Force);
} else if (!onGround && (Mathf.Abs(this.movement.x) > 0.1f)) {
} else if (!onGround && (Mathf.Abs(this.movement.x) > 0.1f) && !playerBehavior.grapplingRope.isGrappling) { // while in air
this.rb.AddForce(move * Vector2.right * airSpeedMultiplier, ForceMode2D.Force);
} else if (!playerBehavior.grapplingRope.isGrappling) { // while grappling
this.rb.AddForce(move * Vector2.right * airSpeedMultiplier * airSpeedMultiplier, ForceMode2D.Force);
}
// decelerate until stopped
if (Mathf.Abs(this.movement.x) < 0.1f)
if (onGround && Mathf.Abs(this.movement.x) < 0.1f)
{
if (Mathf.Abs(rb.velocity.x) > 0.1f) {
float amount = Mathf.Min(
@@ -110,27 +106,6 @@ public class PlayerMovement : MonoBehaviour
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
grappleSurface = col.gameObject;
}
else if (col.tag == "instaDeath")
{
this.stateController.SetDeathCanvasActive(true);
Destroy(this.gameObject);
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
grappleSurface = null;
}
}
bool IsGrounded()
{
if (Physics2D.BoxCast(transform.position, boxSize, 0, -transform.up, maxDistanceFromGround, groundLayer))

View File

@@ -25,22 +25,12 @@ public class Tutorial_GrapplingGun : MonoBehaviour {
[SerializeField] private bool rotateOverTime = true;
[Range(0, 60)][SerializeField] private float rotationSpeed = 4;
[Header("Distance:")]
[SerializeField] private bool hasMaxDistance = false;
[SerializeField] private float maxDistance = 20;
private enum LaunchType {
TransformLaunch,
PhysicsLaunch
}
[Header("Launching:")]
[SerializeField] private bool launchToPoint = true;
[SerializeField] private LaunchType launchType = LaunchType.PhysicsLaunch;
[SerializeField] private float launchSpeed = 1;
[Header("No Launch To Point")]
[SerializeField] private bool autoConfigureDistance = false;
[SerializeField] private float targetDistance = 3;
[SerializeField] private float targetFrequency = 1;
@@ -60,32 +50,12 @@ public class Tutorial_GrapplingGun : MonoBehaviour {
}
void Update() {
// if (Input.GetKeyDown(KeyCode.Mouse0)) {
// SetGrapplePoint();
// } else if (Input.GetKey(KeyCode.Mouse0)) {
// if (grappleRope.enabled) {
// RotateGun(grapplePoint, false);
// } else {
// Vector2 mousePos = m_camera.ScreenToWorldPoint(Input.mousePosition);
// RotateGun(mousePos, true);
// }
// if (launchToPoint && grappleRope.isGrappling) {
// if (launchType == LaunchType.TransformLaunch) {
// Vector2 firePointDistance = firePoint.position - gunHolder.localPosition;
// Vector2 targetPos = grapplePoint - firePointDistance;
// gunHolder.position = Vector2.Lerp(gunHolder.position, targetPos, Time.deltaTime * launchSpeed);
// }
// }
// } else if (Input.GetKeyUp(KeyCode.Mouse0)) {
// ReleaseGrapple();
// } else {
Vector2 mousePos = m_camera.ScreenToWorldPoint(Mouse.current.position.ReadValue());
RotateGun(mousePos, true);
// }
Vector2 mousePos = m_camera.ScreenToWorldPoint(Mouse.current.position.ReadValue());
RotateGun(mousePos, true);
if (grappleRope.isGrappling && !inDistanceRange && Vector2.Distance(grapplePoint, new Vector2(m_rigidBody2D.transform.position.x, m_rigidBody2D.transform.position.y)) < targetDistance) {
print(Vector2.Distance(grapplePoint, new Vector2(m_rigidBody2D.transform.position.x, m_rigidBody2D.transform.position.y)) + ", target: " + targetDistance);
// print(Vector2.Distance(grapplePoint, new Vector2(m_rigidBody2D.transform.position.x, m_rigidBody2D.transform.position.y)) + ", target: " + targetDistance);
inDistanceRange = true;
}
if (inDistanceRange) {
@@ -105,65 +75,17 @@ public class Tutorial_GrapplingGun : MonoBehaviour {
}
// void SetGrapplePoint() {
// Vector2 distanceVector = m_camera.ScreenToWorldPoint(Input.mousePosition) - gunPivot.position;
// // print("clicked" + m_camera.ScreenToWorldPoint(Input.mousePosition));
// // print("distance vector: " + distanceVector);
// if (Physics2D.Raycast(firePoint.position, distanceVector.normalized)) {
// RaycastHit2D _hit = Physics2D.Raycast(firePoint.position, distanceVector.normalized);
// print(_hit.transform.gameObject.name);
// if (_hit.transform.gameObject.layer == grappleLayerNumber || grappleToAll) {
// if (Vector2.Distance(_hit.point, firePoint.position) <= maxDistance || !hasMaxDistance) {
// // print("gunPivot " + gunPivot.position + ", grappling to: " + _hit.point);
// grapplePoint = _hit.point;
// grappleDistanceVector = grapplePoint - (Vector2)gunPivot.position;
// grappleRope.enabled = true;
// }
// }
// }
// }
public void Grapple() {
print("grapple");
// print("grapple");
m_springJoint2D.autoConfigureDistance = false;
m_distanceJoint2D.autoConfigureDistance = false;
if(!launchToPoint && !autoConfigureDistance) {
m_springJoint2D.distance = targetDistance;
print("Sprint Joint Distance:" + m_springJoint2D.distance);
m_springJoint2D.frequency = targetFrequency;
}
if (!launchToPoint) {
if (autoConfigureDistance) {
m_springJoint2D.connectedAnchor = grapplePoint;
m_springJoint2D.enabled = true;
// print("Spring Joint Enabled");
print("Sprint Joint Distance:" + m_springJoint2D.distance);
m_springJoint2D.connectedAnchor = grapplePoint;
m_springJoint2D.enabled = true;
// print("Spring Joint Enabled");
// print("Sprint Joint Distance:" + m_springJoint2D.distance);
m_distanceJoint2D.connectedAnchor = grapplePoint;
}
} else {
switch (launchType) {
case LaunchType.PhysicsLaunch:
m_springJoint2D.connectedAnchor = grapplePoint;
m_distanceJoint2D.connectedAnchor = grapplePoint;
Vector2 distanceVector = firePoint.position - gunHolder.position;
m_springJoint2D.distance = distanceVector.magnitude;
m_springJoint2D.frequency = launchSpeed;
m_springJoint2D.enabled = true;
// m_distanceJoint2D.maxDistanceOnly = false;
m_distanceJoint2D.distance = targetDistance + .5f;
break;
case LaunchType.TransformLaunch:
m_rigidBody2D.gravityScale = 0;
m_rigidBody2D.velocity = Vector2.zero;
break;
}
}
m_distanceJoint2D.connectedAnchor = grapplePoint;
}
public void GrappleToTambourine(GameObject tambourine) {
@@ -209,11 +131,4 @@ public class Tutorial_GrapplingGun : MonoBehaviour {
// print("disabled");
}
private void OnDrawGizmosSelected() {
if (firePoint != null && hasMaxDistance) {
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(firePoint.position, maxDistance);
}
}
}

View File

@@ -62,7 +62,7 @@ public class Tutorial_GrapplingRope : MonoBehaviour {
if (!straightLine) {
float roundedLinePos = Mathf.Round(m_lineRenderer.GetPosition(precision - 1).x * 10.0f) * .01f;
float roundedGrapplePos = Mathf.Round(m_lineRenderer.GetPosition(precision - 1).x * 10.0f) * .01f;
print(roundedLinePos + " / " + roundedGrapplePos);
// print(roundedLinePos + " / " + roundedGrapplePos);
if (roundedLinePos == roundedGrapplePos) {
straightLine = true;
} else {
@@ -87,6 +87,7 @@ public class Tutorial_GrapplingRope : MonoBehaviour {
}
void DrawRopeWaves() {
print(moveTime);
for (int i = 0; i < precision; i++) {
float delta = (float)i / ((float)precision - 1f);
Vector2 offset = Vector2.Perpendicular(grapplingGun.grappleDistanceVector).normalized * ropeAnimationCurve.Evaluate(delta) * waveSize;