From 32528712befef39670f2ef30974e33fa291cebe3 Mon Sep 17 00:00:00 2001 From: arngo <27396817+arngo@users.noreply.github.com> Date: Wed, 20 Jan 2021 18:30:48 -0500 Subject: implemented basic platforming controls and physics as well as ghosts --- Player.gd | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Player.gd (limited to 'Player.gd') diff --git a/Player.gd b/Player.gd new file mode 100644 index 0000000..254aef3 --- /dev/null +++ b/Player.gd @@ -0,0 +1,66 @@ +extends KinematicBody2D + + +signal died(position_record) + + +export var GRAVITY = 1500.0 +export var WALK_SPEED = 400 +export var JUMP_FORCE = 400.0 +export var HELD_JUMP_BOOST = 0.3 + +var velocity = Vector2() +var respawn_position +var position_record = [] + +# Called when the node enters the scene tree for the first time. +func _ready(): + respawn_position = position + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass + + +func _physics_process(delta): + position_record.append(position) + + if Input.is_action_pressed("ui_left") and Input.is_action_pressed("ui_right"): + velocity.x = 0 + elif Input.is_action_pressed("ui_left"): + velocity.x = -WALK_SPEED + elif Input.is_action_pressed("ui_right"): + velocity.x = WALK_SPEED + else: + velocity.x = 0 + + if Input.is_action_just_pressed("jump") and self.is_on_floor(): + velocity.y = -JUMP_FORCE + + if Input.is_action_pressed("jump") and not self.is_on_floor(): + velocity.y += delta * -GRAVITY * HELD_JUMP_BOOST + + if not self.is_on_floor(): + velocity.y += delta * GRAVITY + + if self.is_on_ceiling(): + velocity.y = 1 + + # We don't need to multiply velocity by delta because "move_and_slide" already takes delta time into account. + + # The second parameter of "move_and_slide" is the normal pointing up. + # In the case of a 2D platformer, in Godot, upward is negative y, which translates to -1 as a normal. + move_and_slide(velocity, Vector2(0, -1)) + +func respawn(): + position = respawn_position + position_record = [] + velocity.y = 0 + +func die(): + position_record.append(position) + emit_signal("died", position_record) + respawn() + print('dead') -- cgit v1.2.3