2021-04-15 04:25:22 +01:00
|
|
|
using System;
|
|
|
|
|
using Unity.Mathematics;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using static Unity.Mathematics.math;
|
|
|
|
|
using Random = UnityEngine.Random;
|
|
|
|
|
|
|
|
|
|
public class GridObject : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public float RotationSpeed;
|
|
|
|
|
public float GridOffset;
|
|
|
|
|
public float GridAttraction;
|
|
|
|
|
public float Gravity;
|
|
|
|
|
public float Drag = .1f;
|
|
|
|
|
public float LaunchDrag;
|
|
|
|
|
|
|
|
|
|
private float2 _selfVelocity;
|
|
|
|
|
private float _timeOffset;
|
|
|
|
|
|
|
|
|
|
public Zone Zone { get; set; }
|
|
|
|
|
public Vector3 Velocity { get; set; }
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
_timeOffset = Random.value * 100;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
2025-01-30 01:58:05 +00:00
|
|
|
var t = transform;
|
|
|
|
|
t.localRotation = Quaternion.Euler(sin(Time.time - _timeOffset * RotationSpeed) * 90, 0, cos(Time.time - _timeOffset * RotationSpeed) * 90);
|
|
|
|
|
|
2021-04-15 04:25:22 +01:00
|
|
|
if (Zone == null) return;
|
|
|
|
|
|
|
|
|
|
var position = t.position;
|
|
|
|
|
var gridHeight = Zone.GetHeight(position.Flatland()) + GridOffset;
|
|
|
|
|
Velocity += Vector3.up * (sign(gridHeight - position.y) * GridAttraction * Time.deltaTime);
|
|
|
|
|
Velocity *= max(0, 1 - LaunchDrag * Time.deltaTime);
|
|
|
|
|
var normal = Zone.GetNormal(position.Flatland());
|
|
|
|
|
var force = new float2(normal.x, normal.z);
|
|
|
|
|
var forceMagnitude = lengthsq(force);
|
|
|
|
|
if (forceMagnitude > .001f)
|
|
|
|
|
{
|
|
|
|
|
var fa = 1 / (1 - forceMagnitude) - 1;
|
|
|
|
|
_selfVelocity += normalize(force) * Zone.Settings.GravityStrength * fa * Gravity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_selfVelocity *= max(0, 1 - Drag * Time.deltaTime);
|
|
|
|
|
t.position = position + (Velocity + new Vector3(_selfVelocity.x, 0, _selfVelocity.y)) * Time.deltaTime;
|
|
|
|
|
}
|
|
|
|
|
}
|