-
Notifications
You must be signed in to change notification settings - Fork 2
/
Player.cs
86 lines (68 loc) · 2.21 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Godot;
using System;
public class Player : Area2D
{
// Member variables here, example:
// private int a = 2;
// private string b = "textvar";
[Signal]
public delegate void Hit();
[Export]
public int SPEED = 400;
private Vector2 velocity;
private Vector2 screensize;
private CollisionShape2D collision_shape;
public override void _Ready()
{
collision_shape = GetNode("CollisionShape2D") as CollisionShape2D;
screensize = GetViewport().GetSize();
this.Hide();
}
public override void _Process(float delta)
{
velocity = new Vector2();
if(Input.IsActionPressed("ui_right") || Input.IsKeyPressed((int)KeyList.D)) {
velocity.x += 1;
}
if(Input.IsActionPressed("ui_left") || Input.IsKeyPressed((int)KeyList.A)) {
velocity.x -= 1;
}
if(Input.IsActionPressed("ui_down") || Input.IsKeyPressed((int)KeyList.S)) {
velocity.y += 1;
}
if(Input.IsActionPressed("ui_up") || Input.IsKeyPressed((int)KeyList.W)) {
velocity.y -= 1;
}
var animated_sprite = GetNode("AnimatedSprite") as AnimatedSprite;
if(velocity.Length() > 0) {
velocity = velocity.Normalized() * SPEED;
animated_sprite.Play();
} else {
animated_sprite.Stop();
}
this.Position += velocity * delta;
var posx = Mathf.Clamp(this.Position.x, 0, screensize.x);
var posy = Mathf.Clamp(this.Position.y, 0, screensize.y);
this.SetPosition(new Vector2(posx, posy));
if(velocity.x != 0) {
animated_sprite.Animation = "right";
animated_sprite.FlipH = velocity.x < 0;
animated_sprite.FlipV = false;
} else if(velocity.y != 0) {
animated_sprite.Animation = "up";
animated_sprite.FlipV = velocity.y > 0;
}
}
public void _on_Player_body_entered(Godot.Object body)
{
this.Hide();
EmitSignal("Hit");
collision_shape.Disabled = true;
}
public void start(Vector2 pos)
{
this.Position = pos;
this.Show();
collision_shape.Disabled = false;
}
}