-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cs
50 lines (42 loc) · 1.02 KB
/
Player.cs
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
using Godot;
using System;
public class Player : KinematicBody2D
{
[Export] float speed = 600;
[Export] float gravityStrength = 10;
float currentGravitySpeed;
Vector2 direction;
Vector2 currentSpeed;
public override void _Ready()
{
currentSpeed = new Vector2(0,0);
}
public override void _Process(float delta)
{
GD.Print($"currentSpeed: {currentSpeed}");
}
public override void _PhysicsProcess(float delta)
{
ProcessInput();
ProcessGravity(delta);
ProcessVelocity(delta);
}
public void ProcessInput()
{
direction = Input.IsActionPressed("move_left") ? new Vector2(-1, 0) : direction;
direction = Input.IsActionPressed("move_right") ? new Vector2(1, 0) : direction;
}
public void ProcessGravity(float delta)
{
if (IsOnFloor() == false)
currentGravitySpeed += delta * gravityStrength;
else
currentGravitySpeed = 0;
}
public void ProcessVelocity(float delta)
{
currentSpeed = MoveAndSlide(
linearVelocity: currentSpeed * speed + new Vector2(0, currentGravitySpeed) * delta
);
}
}