-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.cs
59 lines (48 loc) · 1.47 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
using Godot;
using System;
public class Player : Area2D {
[Export]
public int moveSpeed = 300;
[Export]
public float moveVectorAmount = 2f;
private Vector2 _screenSize;
private float extent;
[Signal]
public delegate void Hit();
public override void _Ready() {
_screenSize = GetViewport().Size;
extent = GetNode<Sprite>("Sprite").Texture.GetSize().x / 2;
}
public void Start(Vector2 _position) {
Position = _position;
Show();
GetNode<CollisionShape2D>("CollisionShape2D").Disabled = false;
}
public override void _Process(float delta) {
var velocity = new Vector2();
if(Input.IsActionPressed("ui_right")) {
velocity.x += moveVectorAmount;
}
if(Input.IsActionPressed("ui_left")) {
velocity.x -= moveVectorAmount;
}
if (velocity.Length() > 0) {
velocity = velocity.Normalized() * moveSpeed;
}
Position += velocity * delta;
Position = new Vector2(
x: Mathf.Clamp(Position.x, 0 + extent, _screenSize.x - extent),
y: Mathf.Clamp(Position.y, 0, _screenSize.y)
);
}
private void _on_Player_body_entered(object body) {
Hide();
EmitSignal("Hit");
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", true);
try {
// var b = ()
} catch (Exception) {
GD.Print("ERRO!");
}
}
}