-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.cs
67 lines (56 loc) · 1.6 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Godot;
using System;
public class Player : StaticBody2D
{
// Declare member variables here. Examples:
// private int a = 2;
// private string b = "text";\
[Export]
int Speed = 100;
float speedAccumulator = 0;
int prevVelocity = 0;
Vector2 _screenSize;
// Vector2 _startPos = new Vector2(360, 900);
float _playerWidth;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_screenSize = GetViewport().Size;
Hide();
var sprite = GetNode<Sprite>("Sprite");
var size = sprite.Texture.GetSize();
_playerWidth = size.x;
}
public override void _Process(float delta)
{
var velocity = new Vector2();
if (Input.IsActionPressed("ui_right"))
{
speedAccumulator += 66;
velocity.x += 1;
}
else if (Input.IsActionPressed("ui_left"))
{
speedAccumulator -= 66;
velocity.x -= 1;
}
else
{
speedAccumulator *= 0.7f;
}
velocity = (velocity.Normalized() * Speed);
velocity.x += speedAccumulator;
prevVelocity = (int)velocity.x;
Position += velocity * delta;
Position = new Vector2(
x: Mathf.Clamp(Position.x, 0 + _playerWidth * 2, _screenSize.x - _playerWidth * 2),
y: Mathf.Clamp(Position.y, 0, _screenSize.y)
);
}
public void Start(Vector2 pos)
{
Position = pos;
Show();
GetNode<CollisionShape2D>("CollisionShape2D").Disabled = false;
}
}