aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Assets/Scripts/Controllers/Player/PlayerSegment.cs
blob: 0e8a96919b01c6f591569d644d16094f2eb94d6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using UnityEngine;

namespace Controllers.Player
{
  [RequireComponent(typeof(Rigidbody2D))]
  public class PlayerSegment : MonoBehaviour
  {
    internal float sensitivity;
    internal float verticalSensitivity;
    
    internal bool moveleft;
    internal bool moveright;
    
    [SerializeField] public Control left;
    [SerializeField] public Control right;

    [SerializeField] public Vector3 forceOrigin;

    [SerializeField] public float height;
    [SerializeField] public bool isRoot;
    [SerializeField] public bool isTop;

    [SerializeField] public Transform flipper;

    public Rigidbody2D Rigidbody => _rigidbody != null ? _rigidbody : _rigidbody = GetComponent<Rigidbody2D>();
    private Rigidbody2D _rigidbody;

    private void FixedUpdate()
    {
      if (moveleft)
      {
        Rigidbody.AddForceAtPosition(
          Vector2.left * (sensitivity * (isRoot ? 2f : 1f)) + Vector2.up * (verticalSensitivity * (isRoot ? -1f : 1f)),
          transform.position + (height * forceOrigin));
      }
      if (moveright)
      {
        Rigidbody.AddForceAtPosition(
          Vector2.right * (sensitivity * (isRoot ? 2f : 1f)) + Vector2.up * (verticalSensitivity * (isRoot ? -1f : 1f)),
          transform.position + (height * forceOrigin));
      }
    }
  }

  [Serializable]
  public struct Control
  {
    public KeyCode keyCode;
    public GameObject icon;
    public GameObject leg;
    public GameObject arm;
  }
}