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 --- DeadBody.tscn | 15 +++++++++++++ Ghost.gd | 27 +++++++++++++++++++++++ Ghost.tscn | 18 ++++++++++++++++ GhostSpawner.gd | 20 +++++++++++++++++ Hazard.gd | 21 ++++++++++++++++++ Hazard.tscn | 22 +++++++++++++++++++ Level.tscn | 32 +++++++++++++++++++++++++++- Platform.tscn | 17 +++++++++++++++ Player.gd | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Player.tscn | 18 ++++++++++++++++ project.godot | 9 ++++++++ 11 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 DeadBody.tscn create mode 100644 Ghost.gd create mode 100644 Ghost.tscn create mode 100644 GhostSpawner.gd create mode 100644 Hazard.gd create mode 100644 Hazard.tscn create mode 100644 Platform.tscn create mode 100644 Player.gd create mode 100644 Player.tscn diff --git a/DeadBody.tscn b/DeadBody.tscn new file mode 100644 index 0000000..b0d0062 --- /dev/null +++ b/DeadBody.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://Assets/kenny_complete/Base pack/Player/p1_duck.png" type="Texture" id=1] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 34.8766, 35.7335 ) + +[node name="DeadBody" type="RigidBody2D"] +gravity_scale = 3.49 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 1 ) diff --git a/Ghost.gd b/Ghost.gd new file mode 100644 index 0000000..faee8f3 --- /dev/null +++ b/Ghost.gd @@ -0,0 +1,27 @@ +extends KinematicBody2D + + +var position_record +var position_num = 0 + + +func initialize(position_record): + self.position_record = position_record + + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + + +func _physics_process(delta): + if position_num < position_record.size(): + position = position_record[position_num] + position_num += 1 + else: + queue_free() + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass diff --git a/Ghost.tscn b/Ghost.tscn new file mode 100644 index 0000000..1898866 --- /dev/null +++ b/Ghost.tscn @@ -0,0 +1,18 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://Assets/icon.png" type="Texture" id=1] +[ext_resource path="res://Ghost.gd" type="Script" id=2] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 32, 32 ) + +[node name="Ghost" type="KinematicBody2D"] +modulate = Color( 1, 1, 1, 0.427451 ) +script = ExtResource( 2 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) +disabled = true + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 1 ) diff --git a/GhostSpawner.gd b/GhostSpawner.gd new file mode 100644 index 0000000..2363bc6 --- /dev/null +++ b/GhostSpawner.gd @@ -0,0 +1,20 @@ +extends Node + + +var ghost = load("res://Ghost.tscn") + + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass + + +func _on_Player_died(position_record): + var new_ghost = ghost.instance() + new_ghost.initialize(position_record) + add_child(new_ghost) diff --git a/Hazard.gd b/Hazard.gd new file mode 100644 index 0000000..fa0b01f --- /dev/null +++ b/Hazard.gd @@ -0,0 +1,21 @@ +extends Area2D + + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" + + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass + + +func _on_Hazard_body_entered(body): + if body.has_method("die"): + body.die() diff --git a/Hazard.tscn b/Hazard.tscn new file mode 100644 index 0000000..fe3db5c --- /dev/null +++ b/Hazard.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://Hazard.gd" type="Script" id=1] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 24, 24 ) + +[node name="Hazard" type="Area2D"] +script = ExtResource( 1 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( 24, 24 ) +shape = SubResource( 1 ) + +[node name="ColorRect" type="ColorRect" parent="."] +margin_right = 48.0 +margin_bottom = 48.0 +color = Color( 1, 0, 0, 1 ) +__meta__ = { +"_edit_use_anchors_": false +} +[connection signal="body_entered" from="." to="." method="_on_Hazard_body_entered"] diff --git a/Level.tscn b/Level.tscn index b93703f..c979127 100644 --- a/Level.tscn +++ b/Level.tscn @@ -1,3 +1,33 @@ -[gd_scene format=2] +[gd_scene load_steps=6 format=2] + +[ext_resource path="res://Player.tscn" type="PackedScene" id=1] +[ext_resource path="res://Platform.tscn" type="PackedScene" id=2] +[ext_resource path="res://Hazard.tscn" type="PackedScene" id=3] +[ext_resource path="res://GhostSpawner.gd" type="Script" id=4] +[ext_resource path="res://DeadBody.tscn" type="PackedScene" id=5] [node name="Level" type="Node2D"] + +[node name="Player" parent="." instance=ExtResource( 1 )] +position = Vector2( 512, 440 ) + +[node name="Platform2" parent="." instance=ExtResource( 2 )] +position = Vector2( 128, 400 ) +scale = Vector2( 0.2, 0.1 ) + +[node name="Platform3" parent="." instance=ExtResource( 2 )] +position = Vector2( 456, 304 ) +scale = Vector2( 0.2, 0.101598 ) + +[node name="Hazard" parent="." instance=ExtResource( 3 )] +position = Vector2( 688, 280 ) + +[node name="Platform" parent="." instance=ExtResource( 2 )] +position = Vector2( 0, 472 ) + +[node name="GhostSpawner" type="Node" parent="."] +script = ExtResource( 4 ) + +[node name="DeadBody" parent="." instance=ExtResource( 5 )] +position = Vector2( 237.602, 353.733 ) +[connection signal="died" from="Player" to="GhostSpawner" method="_on_Player_died"] diff --git a/Platform.tscn b/Platform.tscn new file mode 100644 index 0000000..113688c --- /dev/null +++ b/Platform.tscn @@ -0,0 +1,17 @@ +[gd_scene load_steps=2 format=2] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 512, 64 ) + +[node name="Platform" type="StaticBody2D"] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( 512, 64 ) +shape = SubResource( 1 ) + +[node name="ColorRect" type="ColorRect" parent="."] +margin_right = 1024.0 +margin_bottom = 128.0 +__meta__ = { +"_edit_use_anchors_": false +} 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') diff --git a/Player.tscn b/Player.tscn new file mode 100644 index 0000000..1ab64f3 --- /dev/null +++ b/Player.tscn @@ -0,0 +1,18 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://Assets/icon.png" type="Texture" id=1] +[ext_resource path="res://Player.gd" type="Script" id=2] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 32, 32 ) + +[node name="Player" type="KinematicBody2D"] +script = ExtResource( 2 ) +GRAVITY = 1500.0 +JUMP_FORCE = 500.0 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 1 ) diff --git a/project.godot b/project.godot index 1a25130..0b11c47 100644 --- a/project.godot +++ b/project.godot @@ -16,8 +16,17 @@ _global_script_class_icons={ [application] config/name="E-C-Kai" +run/main_scene="res://Level.tscn" config/icon="res://Assets/icon.png" +[input] + +jump={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null) + ] +} + [rendering] quality/driver/driver_name="GLES2" -- cgit v1.2.3