From 65149fcb429a9f92756484c1d3cb485e5f2d8a5f Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Thu, 10 Nov 2022 00:11:21 -0300 Subject: [PATCH] start player control code --- scenes/player/Player.cs | 48 +++++++++++++++++++++++++++++++++++++++ scenes/player/Player.tscn | 5 ++-- 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 scenes/player/Player.cs diff --git a/scenes/player/Player.cs b/scenes/player/Player.cs new file mode 100644 index 0000000..6eda834 --- /dev/null +++ b/scenes/player/Player.cs @@ -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("/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"); + } +} diff --git a/scenes/player/Player.tscn b/scenes/player/Player.tscn index c4e6ce3..8a56b82 100644 --- a/scenes/player/Player.tscn +++ b/scenes/player/Player.tscn @@ -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/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/surface_rising.png" type="Texture" id=8] [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] atlas = ExtResource( 9 ) @@ -131,11 +132,11 @@ extents = Vector2( 6, 6.5 ) [node name="Player" type="KinematicBody2D"] collision_mask = 0 +script = ExtResource( 10 ) [node name="AnimatedSprite" type="AnimatedSprite" parent="."] frames = SubResource( 3 ) animation = "idle" -frame = 1 playing = true [node name="CollisionShape2D" type="CollisionShape2D" parent="."]