summaryrefslogtreecommitdiff
path: root/Player.cs
blob: e0f7fb018f143d9ad6ab63befb63e973888cf464 (plain)
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
55
56
57
58
59
60
61
using Godot;
using System;

public partial class Player : CharacterBody3D
{
    Vector3 vel = new Vector3();
    Vector3 inputDir = new Vector3();
    Vector3 controlDir;
    float gravity = -4.6f;
    bool jump = false;

    public override void _Ready()
    {
        base._Ready();
        Global.player = this;
    }

    public override void _PhysicsProcess(double delta)
    {
        base._PhysicsProcess(delta);
        if (IsOnFloor() && jump) {
            jump = false;
        } else if (IsOnFloor()) {
            vel.Y = 0;
        } else {
            vel.Y += (float) (gravity * delta) * 64; //- 20;
        }
        Vector3 camForward = Global.camera.GlobalTransform.Basis.Z;
        float angleToCam = camForward.SignedAngleTo(Vector3.Forward, Vector3.Up); //angle between control forward and camera forward
		controlDir = inputDir.Normalized().Rotated(Vector3.Down, angleToCam);
        vel.X = controlDir.X * 10f;
        vel.Z = controlDir.Z * 10f;
        Velocity = vel;

        MoveAndSlide();
    }

    public override void _Input(InputEvent @event)
    {
        base._Input(@event);
        if (@event.IsActionPressed("jump"))
        {
            Jump();
        }

        inputDir.X = Input.GetAxis("right", "left");
        inputDir.Z = Input.GetAxis("down", "up");
    }

    private void Jump()
    {
        if (IsOnFloor())
        {
            jump = true;
            vel = new Vector3(Velocity.X, 40f, Velocity.Z);
        } else
        {
            GD.Print("Can't jump, not on floor");
        }
    }
}