static body, rigid body, character body, area2d, gamecontroller, custom signal
This commit is contained in:
parent
16fc558f04
commit
a98540de07
11
Scenes/crate.tscn
Normal file
11
Scenes/crate.tscn
Normal file
@ -0,0 +1,11 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://cd7fv3pwgr880"]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_ebmjs"]
|
||||
|
||||
[node name="RigidBody2D3" type="RigidBody2D"]
|
||||
rotation = -0.544116
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
shape = SubResource("RectangleShape2D_ebmjs")
|
||||
debug_color = Color(0.796822, 0.420181, 0.140254, 0.42)
|
50
Scenes/game.tscn
Normal file
50
Scenes/game.tscn
Normal file
@ -0,0 +1,50 @@
|
||||
[gd_scene load_steps=7 format=3 uid="uid://c6mxse0hqjro7"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://dsryf6gxqcm1k" path="res://Scripts/character_body_2d.gd" id="1_ebmjs"]
|
||||
[ext_resource type="Script" uid="uid://tno7aku0yyke" path="res://Scripts/gamecontroller.gd" id="1_wrm1d"]
|
||||
[ext_resource type="PackedScene" uid="uid://cd7fv3pwgr880" path="res://Scenes/crate.tscn" id="3_3dryh"]
|
||||
[ext_resource type="PackedScene" uid="uid://b5d8himq5sttt" path="res://Scenes/trigger.tscn" id="4_wowpa"]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_2poj3"]
|
||||
size = Vector2(50, 20)
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_2poj3"]
|
||||
|
||||
[node name="Game" type="Node2D"]
|
||||
script = ExtResource("1_wrm1d")
|
||||
|
||||
[node name="StaticBody2D" type="StaticBody2D" parent="."]
|
||||
position = Vector2(595, 337)
|
||||
scale = Vector2(1.50487, 1.09333)
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
|
||||
shape = SubResource("RectangleShape2D_2poj3")
|
||||
|
||||
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
|
||||
position = Vector2(576, 303)
|
||||
script = ExtResource("1_ebmjs")
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
|
||||
shape = SubResource("CircleShape2D_2poj3")
|
||||
debug_color = Color(0.613557, 0.363647, 0.991688, 0.42)
|
||||
|
||||
[node name="RigidBody2D3" parent="." instance=ExtResource("3_3dryh")]
|
||||
position = Vector2(596, 262)
|
||||
|
||||
[node name="RigidBody2D4" parent="." instance=ExtResource("3_3dryh")]
|
||||
position = Vector2(616, 222)
|
||||
|
||||
[node name="RigidBody2D5" parent="." instance=ExtResource("3_3dryh")]
|
||||
position = Vector2(574, 222)
|
||||
|
||||
[node name="Area2D2" parent="." instance=ExtResource("4_wowpa")]
|
||||
position = Vector2(511, 397)
|
||||
effect = "destroy"
|
||||
|
||||
[node name="Area2D3" parent="." instance=ExtResource("4_wowpa")]
|
||||
position = Vector2(684, 400)
|
||||
|
||||
[connection signal="areatrigger" from="Area2D2" to="." method="_on_areatrigger"]
|
||||
[connection signal="areatrigger" from="Area2D3" to="." method="_on_areatrigger"]
|
18
Scenes/trigger.tscn
Normal file
18
Scenes/trigger.tscn
Normal file
@ -0,0 +1,18 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://b5d8himq5sttt"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bxcno1cpdhsos" path="res://Scripts/trigger.gd" id="1_vcq5i"]
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_wrm1d"]
|
||||
radius = 26.2488
|
||||
|
||||
[node name="Area2D2" type="Area2D"]
|
||||
scale = Vector2(2.24149, 2.031)
|
||||
script = ExtResource("1_vcq5i")
|
||||
effect = "powerup"
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
shape = SubResource("CircleShape2D_wrm1d")
|
||||
debug_color = Color(0.439885, 0.598937, 0, 0.42)
|
||||
|
||||
[connection signal="body_entered" from="." to="." method="_on_body_entered"]
|
29
Scripts/character_body_2d.gd
Normal file
29
Scripts/character_body_2d.gd
Normal file
@ -0,0 +1,29 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
var direction := Input.get_axis("ui_left", "ui_right")
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
|
1
Scripts/character_body_2d.gd.uid
Normal file
1
Scripts/character_body_2d.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://dsryf6gxqcm1k
|
20
Scripts/gamecontroller.gd
Normal file
20
Scripts/gamecontroller.gd
Normal file
@ -0,0 +1,20 @@
|
||||
extends Node2D
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_areatrigger(effect, body) -> void:
|
||||
print("Game controller knows about trigger")
|
||||
if effect == "destroy":
|
||||
print("Destroy this thing")
|
||||
body.queue_free()
|
||||
if effect == "powerup":
|
||||
print("Power up the player")
|
1
Scripts/gamecontroller.gd.uid
Normal file
1
Scripts/gamecontroller.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://tno7aku0yyke
|
19
Scripts/trigger.gd
Normal file
19
Scripts/trigger.gd
Normal file
@ -0,0 +1,19 @@
|
||||
extends Area2D
|
||||
|
||||
@export var effect = "destroy"
|
||||
|
||||
signal areatrigger(effect, Object)
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
print("Trigger entered")
|
||||
#broadcast to controller
|
||||
areatrigger.emit(effect, body)
|
1
Scripts/trigger.gd.uid
Normal file
1
Scripts/trigger.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://bxcno1cpdhsos
|
@ -11,5 +11,13 @@ config_version=5
|
||||
[application]
|
||||
|
||||
config/name="AutumnGame"
|
||||
run/main_scene="uid://c6mxse0hqjro7"
|
||||
config/features=PackedStringArray("4.4", "Forward Plus")
|
||||
config/icon="res://icon.svg"
|
||||
|
||||
[file_customization]
|
||||
|
||||
folder_colors={
|
||||
"res://Scenes/": "pink",
|
||||
"res://Scripts/": "orange"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user