-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlatformerPlayer.cs
More file actions
80 lines (72 loc) · 1.87 KB
/
PlatformerPlayer.cs
File metadata and controls
80 lines (72 loc) · 1.87 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using Godot;
using System;
public class PlatformerPlayer : KinematicBody2D
{
private enum State
{
IDLE,
RUN,
JUMP
}
[Export] public int RunSpeed = 200;
[Export] public int JumpSpeed = 350;
[Export] public int Gravity = 1200;
private State _state = State.IDLE;
private Vector2 _velocity;
private AnimatedSprite _anim;
private bool _facingLeft = false;
public override void _Ready()
{
_anim = GetNode<AnimatedSprite>("AnimatedSprite");
}
private void _GetInput()
{
_velocity.x = 0;
var left = Input.IsActionPressed("left");
var right = Input.IsActionPressed("right");
var jump = Input.IsActionPressed("jump");
if (jump && IsOnFloor())
{
_velocity.y = -JumpSpeed;
_state = State.JUMP;
}
if (left)
{
_state = State.RUN;
_velocity.x -= RunSpeed;
_facingLeft = true;
}
if (right)
{
_state = State.RUN;
_velocity.x += RunSpeed;
_facingLeft = false;
}
//_facingLeft = _velocity.x < 0;
if (!right && !left && _state == State.RUN)
_state = State.IDLE;
}
public override void _Process(float delta)
{
_GetInput();
if (_state == State.IDLE)
{
_anim.Playing = false;
_anim.Frame = 0;
}
else
{
_anim.Playing = true;
}
_anim.FlipH = _facingLeft;
}
public override void _PhysicsProcess(float delta)
{
_velocity.y += Gravity * delta;
if (_state == State.JUMP && IsOnFloor())
_state = State.IDLE;
_velocity = MoveAndSlide(_velocity, new Vector2(0, -1));
if (Position.y > 600)
GetTree().ReloadCurrentScene();
}
}