BGJ-2022.1/Assets/Scripts/Platforms/MovingPlatform.cs

112 lines
3.4 KiB
C#
Raw Permalink Normal View History

2022-02-25 12:36:45 +03:00
using System;
2022-02-23 13:47:46 +03:00
using Movement;
using UnityEngine;
namespace Platforms
{
public class MovingPlatform : MonoBehaviour, IMovement
{
2022-02-25 12:36:45 +03:00
public float xOffset;
2022-02-23 15:05:30 +03:00
public float yOffset;
2022-02-25 22:14:54 +03:00
public float speed;
2022-02-23 15:05:30 +03:00
2022-02-25 12:36:45 +03:00
private Vector3 _originalPos;
private Vector3 _futurePos;
private const float VerificationOffset = 0.04f;
private bool _goingToFuturePos;
2022-02-23 15:05:30 +03:00
private Rigidbody2D _platformRigidbody;
2022-02-25 23:33:29 +03:00
private CollisionChecker _movingPlatformTrigger;
2022-02-23 15:05:30 +03:00
private void Awake()
{
_platformRigidbody = GetComponent<Rigidbody2D>();
2022-02-25 23:33:29 +03:00
_movingPlatformTrigger = GetComponentInChildren<CollisionChecker>();
2022-02-23 15:05:30 +03:00
}
2022-02-25 12:36:45 +03:00
private void Start()
{
_originalPos = transform.position;
_futurePos = new Vector3(_originalPos.x + xOffset, _originalPos.y + yOffset, _originalPos.z);
_goingToFuturePos = true;
}
2022-02-25 22:14:54 +03:00
private void FixedUpdate()
2022-02-23 15:05:30 +03:00
{
if (!IsPaused)
Move(BaseSpeed);
}
2022-02-23 13:47:46 +03:00
// PAUSING METHODS
2022-02-25 12:36:45 +03:00
2022-02-23 15:05:30 +03:00
public bool IsPaused { get; private set; }
2022-02-25 12:36:45 +03:00
2022-02-23 13:47:46 +03:00
public void Pause()
{
2022-02-23 15:05:30 +03:00
IsPaused = true;
_platformRigidbody.simulated = !IsPaused;
2022-02-23 13:47:46 +03:00
}
public void Resume()
{
2022-02-23 15:05:30 +03:00
IsPaused = false;
_platformRigidbody.simulated = !IsPaused;
2022-02-23 13:47:46 +03:00
}
2022-02-25 12:36:45 +03:00
2022-02-23 13:47:46 +03:00
// MOVEMENT METHODS
public float BaseSpeed { get; set; }
2022-02-23 15:05:30 +03:00
2022-02-23 13:47:46 +03:00
public void Move(float value)
{
2022-02-25 23:33:29 +03:00
//var position = transform.position;
2022-02-25 12:36:45 +03:00
switch (_goingToFuturePos)
{
case true:
2022-02-25 23:33:29 +03:00
//_platformRigidbody.MovePosition(new Vector2(position.x + speed * GetDecision(xOffset),
//position.y + speed * GetDecision(yOffset)));
transform.position = Vector3.MoveTowards(transform.position, _futurePos, speed * Time.deltaTime);
2022-02-25 12:36:45 +03:00
if (Math.Abs(_futurePos.x - transform.position.x) < VerificationOffset &&
Math.Abs(_futurePos.y - transform.position.y) < VerificationOffset)
_goingToFuturePos = false;
break;
case false:
2022-02-25 23:33:29 +03:00
//_platformRigidbody.MovePosition(new Vector2(position.x + speed * -GetDecision(xOffset),
//position.y + speed * -GetDecision(yOffset)));
transform.position = Vector3.MoveTowards(transform.position, _originalPos, speed * Time.deltaTime);
2022-02-25 12:36:45 +03:00
if (Math.Abs(_originalPos.x - transform.position.x) < VerificationOffset &&
Math.Abs(_originalPos.y - transform.position.y) < VerificationOffset)
_goingToFuturePos = true;
break;
}
2022-02-23 13:47:46 +03:00
}
2022-02-25 22:14:54 +03:00
private static float GetDecision(float value)
{
switch (value)
{
case < 0:
return -1.0f;
case > 0:
return 1.0f;
}
return 0.0f;
}
2022-02-26 11:10:26 +03:00
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Player"))
other.transform.SetParent(transform);
}
private void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.CompareTag("Player"))
other.transform.SetParent(null);
}
2022-02-23 13:47:46 +03:00
}
2022-02-26 11:10:26 +03:00
}