start player control code

This commit is contained in:
Chris Cromer 2022-11-10 00:11:21 -03:00
parent d5a6585999
commit 65149fcb42
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
2 changed files with 51 additions and 2 deletions

48
scenes/player/Player.cs Normal file
View File

@ -0,0 +1,48 @@
using Godot;
public class Player : KinematicBody2D
{
[Export]
private bool _invincible { get; set; }
private Event _eventBus;
private Vector2 _velocity;
public override void _Ready()
{
_eventBus = GetNode<Event>("/root/Event");
_eventBus.Connect("PlayerTouched", this, "PlayerTouched");
_invincible = false;
_velocity = new Vector2();
}
public override void _PhysicsProcess(float delta)
{
_velocity.x = 0;
if (Input.IsActionPressed("right"))
{
_velocity.x += 50;
}
if (Input.IsActionPressed("left")) {
_velocity.x -= 50;
}
_velocity = MoveAndSlide(_velocity, Vector2.Up);
var count = GetSlideCount();
for (var i = 0; i < count; i++)
{
KinematicCollision2D collision = GetSlideCollision(i);
var collider = (Node) collision.Collider;
if (collider.IsInGroup("Enemy"))
{
_eventBus.EmitSignal("PlayerTouched");
}
}
}
private void PlayerTouched() {
GD.Print("Touched");
}
}

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=29 format=2] [gd_scene load_steps=30 format=2]
[ext_resource path="res://assets/player/idle.png" type="Texture" id=1] [ext_resource path="res://assets/player/idle.png" type="Texture" id=1]
[ext_resource path="res://assets/player/death.png" type="Texture" id=2] [ext_resource path="res://assets/player/death.png" type="Texture" id=2]
@ -9,6 +9,7 @@
[ext_resource path="res://assets/player/walking.png" type="Texture" id=7] [ext_resource path="res://assets/player/walking.png" type="Texture" id=7]
[ext_resource path="res://assets/player/surface_rising.png" type="Texture" id=8] [ext_resource path="res://assets/player/surface_rising.png" type="Texture" id=8]
[ext_resource path="res://assets/player/climbing.png" type="Texture" id=9] [ext_resource path="res://assets/player/climbing.png" type="Texture" id=9]
[ext_resource path="res://scenes/player/Player.cs" type="Script" id=10]
[sub_resource type="AtlasTexture" id=18] [sub_resource type="AtlasTexture" id=18]
atlas = ExtResource( 9 ) atlas = ExtResource( 9 )
@ -131,11 +132,11 @@ extents = Vector2( 6, 6.5 )
[node name="Player" type="KinematicBody2D"] [node name="Player" type="KinematicBody2D"]
collision_mask = 0 collision_mask = 0
script = ExtResource( 10 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."] [node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 3 ) frames = SubResource( 3 )
animation = "idle" animation = "idle"
frame = 1
playing = true playing = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]