add some enemy behavior prototypes

This commit is contained in:
Chris Cromer 2022-07-21 00:05:08 -04:00
parent 31003a2863
commit 948f4ce911
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
20 changed files with 268 additions and 34 deletions

View File

@ -0,0 +1,38 @@
extends KinematicBody2D
signal player_touched
var velocity = Vector2()
export var direction = -1
export var detect_edges = true
export var speed = 25
export var gravity = 9.8
func _ready() -> void:
if direction == 1:
$AnimatedSprite.flip_h = true
$FloorChecker.position.x = $CollisionShape2D.shape.get_extents().x * direction
$FloorChecker.enabled = detect_edges
func _physics_process(_delta: float) -> void:
if is_on_wall() or not $FloorChecker.is_colliding() and is_on_floor() and $FloorChecker.enabled:
direction *= -1
$AnimatedSprite.flip_h = not $AnimatedSprite.flip_h
$FloorChecker.position.x = $CollisionShape2D.shape.get_extents().x * direction
velocity.y += gravity
velocity.x = speed * direction
velocity = move_and_slide(velocity, Vector2.UP)
for i in get_slide_count():
var collision = get_slide_collision(i)
if collision.collider.name == "Player":
emit_signal("player_touched")
func squash() -> void:
queue_free()

View File

@ -0,0 +1,27 @@
extends KinematicBody2D
# Declare member variables here. Examples:
# var a: int = 2
# var b: String = "text"
# 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 _physics_process(delta: float) -> void:
var velocity = Vector2()
velocity.x += 1 * 50
#velocity.y -= 1 * 50
velocity = move_and_slide(velocity, Vector2(0, 0), true, 2)
for i in get_slide_count():
var collision = get_slide_collision(i)
#if collision.collider.name == "Player":
# print("Blightwing: " + String(OS.get_unix_time()) + " " + collision.collider.name)
func _on_Area2D_body_entered(body: Node) -> void:
print(body.name)

View File

@ -25,14 +25,13 @@ animations = [ {
[sub_resource type="RectangleShape2D" id=5] [sub_resource type="RectangleShape2D" id=5]
extents = Vector2( 5, 5 ) extents = Vector2( 5, 5 )
[node name="Blightwing" type="KinematicBody2D"] [node name="Blightwing" type="KinematicBody2D" groups=["enemy"]]
collision_layer = 8 collision_layer = 8
collision_mask = 11 collision_mask = 11
[node name="AnimatedSprite" type="AnimatedSprite" parent="."] [node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 4 ) frames = SubResource( 4 )
animation = "fly" animation = "fly"
frame = 1
playing = true playing = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]

View File

@ -0,0 +1,27 @@
extends KinematicBody2D
signal player_touched
onready var start_position = position
var velocity = Vector2()
export var speed = 50.0
export var fall_speed = 75.0
var return_to_start: bool = false
func _physics_process(delta: float) -> void:
if return_to_start:
position = position.move_toward(start_position, speed * delta)
else:
var collision = move_and_collide(Vector2(0, (position.y + fall_speed) * delta))
if collision:
return_to_start = true
$AnimatedSprite.play("normal")
if collision.collider.name == "Player":
emit_signal("player_touched")
if position.y <= start_position.y:
return_to_start = false
$AnimatedSprite.play("angry")

View File

@ -1,6 +1,7 @@
[gd_scene load_steps=6 format=2] [gd_scene load_steps=7 format=2]
[ext_resource path="res://assets/characters/characters.png" type="Texture" id=1] [ext_resource path="res://assets/characters/characters.png" type="Texture" id=1]
[ext_resource path="res://characters/enemies/blockface/Blockface.gd" type="Script" id=2]
[sub_resource type="AtlasTexture" id=1] [sub_resource type="AtlasTexture" id=1]
atlas = ExtResource( 1 ) atlas = ExtResource( 1 )
@ -26,9 +27,10 @@ animations = [ {
[sub_resource type="RectangleShape2D" id=4] [sub_resource type="RectangleShape2D" id=4]
extents = Vector2( 9, 9 ) extents = Vector2( 9, 9 )
[node name="Blockface" type="KinematicBody2D"] [node name="Blockface" type="KinematicBody2D" groups=["enemy", "rideable"]]
collision_layer = 8 collision_layer = 8
collision_mask = 11 collision_mask = 11
script = ExtResource( 2 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."] [node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 3 ) frames = SubResource( 3 )

View File

@ -0,0 +1 @@
extends "res://characters/enemies/WalkingEnemy.gd"

View File

@ -1,6 +1,7 @@
[gd_scene load_steps=7 format=2] [gd_scene load_steps=8 format=2]
[ext_resource path="res://assets/characters/characters.png" type="Texture" id=1] [ext_resource path="res://assets/characters/characters.png" type="Texture" id=1]
[ext_resource path="res://characters/enemies/dreadtooth/Dreadtooth.gd" type="Script" id=2]
[sub_resource type="AtlasTexture" id=1] [sub_resource type="AtlasTexture" id=1]
atlas = ExtResource( 1 ) atlas = ExtResource( 1 )
@ -28,17 +29,26 @@ animations = [ {
} ] } ]
[sub_resource type="RectangleShape2D" id=5] [sub_resource type="RectangleShape2D" id=5]
extents = Vector2( 7.5, 8.5 ) extents = Vector2( 7.5, 6.5 )
[node name="Dreadtooth" type="KinematicBody2D"] [node name="Dreadtooth" type="KinematicBody2D" groups=["enemy"]]
collision_layer = 8 collision_layer = 8
collision_mask = 11 collision_mask = 11
script = ExtResource( 2 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."] [node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 4 ) frames = SubResource( 4 )
animation = "walk" animation = "walk"
playing = true frame = 1
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( -0.5, 3.5 ) position = Vector2( -0.5, 5.5 )
shape = SubResource( 5 ) shape = SubResource( 5 )
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
polygon = PoolVector2Array( 0, -7, 3, -4, 3, -1, -4, -1, -4, -4, -1, -7 )
[node name="FloorChecker" type="RayCast2D" parent="."]
enabled = true
cast_to = Vector2( 0, 20 )
collision_mask = 10

View File

@ -21,12 +21,13 @@ animations = [ {
[sub_resource type="RectangleShape2D" id=4] [sub_resource type="RectangleShape2D" id=4]
extents = Vector2( 5.5, 7.5 ) extents = Vector2( 5.5, 7.5 )
[node name="Gravevine" type="KinematicBody2D"] [node name="Gravevine" type="KinematicBody2D" groups=["enemy"]]
collision_layer = 8 collision_layer = 8
collision_mask = 11 collision_mask = 11
[node name="AnimatedSprite" type="AnimatedSprite" parent="."] [node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 3 ) frames = SubResource( 3 )
frame = 1
playing = true playing = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]

View File

@ -0,0 +1 @@
extends "res://characters/enemies/WalkingEnemy.gd"

View File

@ -1,6 +1,7 @@
[gd_scene load_steps=7 format=2] [gd_scene load_steps=8 format=2]
[ext_resource path="res://assets/characters/characters.png" type="Texture" id=1] [ext_resource path="res://assets/characters/characters.png" type="Texture" id=1]
[ext_resource path="res://characters/enemies/shelly/Shelly.gd" type="Script" id=2]
[sub_resource type="AtlasTexture" id=1] [sub_resource type="AtlasTexture" id=1]
atlas = ExtResource( 1 ) atlas = ExtResource( 1 )
@ -28,11 +29,12 @@ animations = [ {
} ] } ]
[sub_resource type="RectangleShape2D" id=5] [sub_resource type="RectangleShape2D" id=5]
extents = Vector2( 7.5, 7 ) extents = Vector2( 7.5, 6.5 )
[node name="Shelly" type="KinematicBody2D"] [node name="Shelly" type="KinematicBody2D" groups=["enemy", "squashable"]]
collision_layer = 8 collision_layer = 8
collision_mask = 11 collision_mask = 11
script = ExtResource( 2 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."] [node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 4 ) frames = SubResource( 4 )
@ -40,5 +42,10 @@ animation = "walk"
playing = true playing = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( 1, 5 ) position = Vector2( 1, 5.5 )
shape = SubResource( 5 ) shape = SubResource( 5 )
[node name="FloorChecker" type="RayCast2D" parent="."]
enabled = true
cast_to = Vector2( 0, 20 )
collision_mask = 10

View File

@ -17,7 +17,7 @@ animations = [ {
[sub_resource type="CircleShape2D" id=3] [sub_resource type="CircleShape2D" id=3]
radius = 9.05539 radius = 9.05539
[node name="Spikeball" type="KinematicBody2D"] [node name="Spikeball" type="KinematicBody2D" groups=["enemy"]]
collision_layer = 8 collision_layer = 8
collision_mask = 11 collision_mask = 11

View File

@ -0,0 +1 @@
extends "res://characters/enemies/WalkingEnemy.gd"

View File

@ -1,6 +1,7 @@
[gd_scene load_steps=7 format=2] [gd_scene load_steps=8 format=2]
[ext_resource path="res://assets/characters/characters.png" type="Texture" id=1] [ext_resource path="res://assets/characters/characters.png" type="Texture" id=1]
[ext_resource path="res://characters/enemies/super_shelly/SuperShelly.gd" type="Script" id=2]
[sub_resource type="AtlasTexture" id=1] [sub_resource type="AtlasTexture" id=1]
atlas = ExtResource( 1 ) atlas = ExtResource( 1 )
@ -28,11 +29,12 @@ animations = [ {
} ] } ]
[sub_resource type="RectangleShape2D" id=5] [sub_resource type="RectangleShape2D" id=5]
extents = Vector2( 12, 11 ) extents = Vector2( 12, 10.5 )
[node name="SuperShelly" type="KinematicBody2D"] [node name="SuperShelly" type="KinematicBody2D" groups=["enemy", "squashable"]]
collision_layer = 8 collision_layer = 8
collision_mask = 11 collision_mask = 11
script = ExtResource( 2 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."] [node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 4 ) frames = SubResource( 4 )
@ -41,5 +43,10 @@ frame = 1
playing = true playing = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( 0, 1 ) position = Vector2( 0, 1.5 )
shape = SubResource( 5 ) shape = SubResource( 5 )
[node name="FloorChecker" type="RayCast2D" parent="."]
enabled = true
cast_to = Vector2( 0, 20 )
collision_mask = 10

View File

@ -9,10 +9,10 @@
[ext_resource path="res://characters/player/states/Fall.gdns" type="Script" id=7] [ext_resource path="res://characters/player/states/Fall.gdns" type="Script" id=7]
[sub_resource type="RectangleShape2D" id=1] [sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 7, 12 ) extents = Vector2( 6.5, 11.5 )
[node name="Player" type="KinematicBody2D"] [node name="Player" type="KinematicBody2D"]
collision_mask = 2 collision_mask = 10
script = ExtResource( 5 ) script = ExtResource( 5 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."] [node name="AnimatedSprite" type="AnimatedSprite" parent="."]
@ -22,7 +22,7 @@ centered = false
flip_h = true flip_h = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( 12, 12 ) position = Vector2( 12.5, 12.5 )
shape = SubResource( 1 ) shape = SubResource( 1 )
[node name="StateMachine" type="Node" parent="."] [node name="StateMachine" type="Node" parent="."]
@ -40,3 +40,9 @@ script = ExtResource( 6 )
[node name="Fall" type="Node" parent="StateMachine"] [node name="Fall" type="Node" parent="StateMachine"]
script = ExtResource( 7 ) script = ExtResource( 7 )
[node name="PlatformDetector" type="RayCast2D" parent="."]
position = Vector2( 12, 0 )
enabled = true
cast_to = Vector2( 0, 32 )
collision_mask = 24

View File

@ -22,16 +22,16 @@
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
22,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,24 22,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,24

View File

@ -13,9 +13,9 @@ dest_files=[ "res://.import/PrototypeEnemies.tmx-5728a1d35b5e3ba88a2df2406b1cd93
custom_properties=true custom_properties=true
tile_metadata=false tile_metadata=false
uv_clip=true uv_clip=false
y_sort=false y_sort=false
image_flags=7 image_flags=0
collision_layer=2 collision_layer=2
collision_mask=0 collision_mask=0
embed_internal_images=false embed_internal_images=false

View File

@ -15,7 +15,8 @@
[node name="PrototypeEnemies" type="Node"] [node name="PrototypeEnemies" type="Node"]
[node name="Player" parent="." instance=ExtResource( 2 )] [node name="Player" parent="." instance=ExtResource( 2 )]
position = Vector2( 80, 488 ) position = Vector2( 156, 438 )
collision/safe_margin = 0.12
[node name="Camera2D" type="Camera2D" parent="Player"] [node name="Camera2D" type="Camera2D" parent="Player"]
current = true current = true
@ -49,6 +50,7 @@ centered = false
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="Enemies" type="Node" parent="."] [node name="Enemies" type="Node" parent="."]
unique_name_in_owner = true
[node name="Spikeball" parent="Enemies" instance=ExtResource( 8 )] [node name="Spikeball" parent="Enemies" instance=ExtResource( 8 )]
position = Vector2( 106, 547 ) position = Vector2( 106, 547 )
@ -57,16 +59,25 @@ position = Vector2( 106, 547 )
position = Vector2( 92, 516 ) position = Vector2( 92, 516 )
[node name="Blockface" parent="Enemies" instance=ExtResource( 10 )] [node name="Blockface" parent="Enemies" instance=ExtResource( 10 )]
position = Vector2( 169, 518 ) position = Vector2( 206, 404 )
[node name="Dreadtooth" parent="Enemies" instance=ExtResource( 6 )] [node name="Dreadtooth" parent="Enemies" instance=ExtResource( 6 )]
position = Vector2( 212, 546 ) position = Vector2( 316, 379 )
[node name="Dreadtooth2" parent="Enemies" instance=ExtResource( 6 )]
position = Vector2( 350, 263 )
direction = 1
detect_edges = false
[node name="Gravevine" parent="Enemies" instance=ExtResource( 5 )] [node name="Gravevine" parent="Enemies" instance=ExtResource( 5 )]
position = Vector2( 250, 550 ) position = Vector2( 250, 550 )
[node name="Shelly" parent="Enemies" instance=ExtResource( 9 )] [node name="Shelly" parent="Enemies" instance=ExtResource( 9 )]
position = Vector2( 138, 546 ) position = Vector2( 247, 439 )
[node name="SuperShelly" parent="Enemies" instance=ExtResource( 7 )] [node name="SuperShelly" parent="Enemies" instance=ExtResource( 7 )]
position = Vector2( 62, 546 ) position = Vector2( 62, 546 )
[connection signal="player_touched" from="Enemies/Blockface" to="Player" method="_on_player_touched"]
[connection signal="player_touched" from="Enemies/Shelly" to="Player" method="_on_player_touched"]
[connection signal="player_touched" from="Enemies/SuperShelly" to="Player" method="_on_player_touched"]

View File

@ -101,6 +101,7 @@ Send={
2d_physics/layer_1="Player" 2d_physics/layer_1="Player"
2d_physics/layer_2="Tiles" 2d_physics/layer_2="Tiles"
2d_physics/layer_4="Enemies" 2d_physics/layer_4="Enemies"
2d_physics/layer_5="Platforms"
[physics] [physics]

View File

@ -6,6 +6,8 @@
#include <SceneTree.hpp> #include <SceneTree.hpp>
#include <Texture.hpp> #include <Texture.hpp>
#include <Viewport.hpp> #include <Viewport.hpp>
#include <RayCast2D.hpp>
#include <KinematicCollision2D.hpp>
using namespace godot; using namespace godot;
using namespace player; using namespace player;
@ -16,9 +18,11 @@ void Player::_register_methods()
register_method("_physics_process", &Player::_physics_process); register_method("_physics_process", &Player::_physics_process);
register_method("set_velocity", &Player::set_velocity); register_method("set_velocity", &Player::set_velocity);
register_method("get_velocity", &Player::get_velocity); register_method("get_velocity", &Player::get_velocity);
register_method("_on_player_touched", &Player::_on_player_touched);
//register_property<Player, Ref<SpriteFrames>>("sprite_frames", &Player::set_sprite_frames, &Player::get_sprite_frames, Ref<SpriteFrames>(), GODOT_METHOD_RPC_MODE_DISABLED, GODOT_PROPERTY_USAGE_DEFAULT, GODOT_PROPERTY_HINT_RESOURCE_TYPE, String("SpriteFrames")); //register_property<Player, Ref<SpriteFrames>>("sprite_frames", &Player::set_sprite_frames, &Player::get_sprite_frames, Ref<SpriteFrames>(), GODOT_METHOD_RPC_MODE_DISABLED, GODOT_PROPERTY_USAGE_DEFAULT, GODOT_PROPERTY_HINT_RESOURCE_TYPE, String("SpriteFrames"));
register_property<Player, float>("speed", &Player::set_speed, &Player::get_speed, player::speed); register_property<Player, float>("speed", &Player::set_speed, &Player::get_speed, player::speed);
register_property<Player, float>("jump_force", &Player::set_jump_force, &Player::get_jump_force, player::jump_force); register_property<Player, float>("jump_force", &Player::set_jump_force, &Player::get_jump_force, player::jump_force);
register_property<Player, float>("bounce_force", &Player::set_bounce_force, &Player::get_bounce_force, player::bounce_force);
register_property<Player, float>("gravity", &Player::set_gravity, &Player::get_gravity, player::gravity); register_property<Player, float>("gravity", &Player::set_gravity, &Player::get_gravity, player::gravity);
register_property<Player, float>("run_speed", &Player::set_run_speed, &Player::get_run_speed, player::run_speed); register_property<Player, float>("run_speed", &Player::set_run_speed, &Player::get_run_speed, player::run_speed);
register_property<Player, bool>("double_jump", &Player::set_double_jump, &Player::get_double_jump, player::double_jump); register_property<Player, bool>("double_jump", &Player::set_double_jump, &Player::get_double_jump, player::double_jump);
@ -42,6 +46,7 @@ void Player::_init()
//sprite_frames = _resource_loader->load(player::sprite_frames); //sprite_frames = _resource_loader->load(player::sprite_frames);
set_speed(player::speed); set_speed(player::speed);
set_jump_force(player::jump_force); set_jump_force(player::jump_force);
set_bounce_force(player::bounce_force);
set_gravity(player::gravity); set_gravity(player::gravity);
set_run_speed(player::run_speed); set_run_speed(player::run_speed);
set_double_jump(player::double_jump); set_double_jump(player::double_jump);
@ -73,7 +78,7 @@ void Player::_ready()
WARN_PRINT("Middleground not found!"); WARN_PRINT("Middleground not found!");
} }
auto object_node = get_tree()->get_root()->get_node("Main")->find_node("Monitor"); auto object_node = get_tree()->get_root()->get_node("Main")->find_node("Monitor", true);
if (object_node != nullptr) if (object_node != nullptr)
{ {
auto state = get_node("StateMachine")->get_child(0); auto state = get_node("StateMachine")->get_child(0);
@ -99,9 +104,54 @@ void Player::_physics_process(float delta)
{ {
velocity.y += get_gravity(); velocity.y += get_gravity();
velocity = move_and_slide(velocity, Vector2::UP, true); auto snap_vector = Vector2::ZERO;
if (!is_on_floor())
{
snap_vector = Vector2::DOWN * 20.0;
}
auto platform_detector = get_node<RayCast2D>("PlatformDetector");
auto is_on_platform = platform_detector->is_colliding();
velocity = move_and_slide_with_snap(velocity, snap_vector, Vector2::UP, !is_on_platform, 4, 0.9, false);
//velocity = move_and_slide(velocity, Vector2::UP, !is_on_platform);
velocity.x = Math::lerp((float) velocity.x, (float) 0, (float) 0.2); velocity.x = Math::lerp((float) velocity.x, (float) 0, (float) 0.2);
auto count = get_slide_count();
for (int64_t i = 0; i < count; i++)
{
auto collision = get_slide_collision(i);
auto collision_object = collision->get_collider();
auto collider = Object::cast_to<Node>(collision_object);
if (collider->is_in_group("squashable") && Vector2::UP.dot(collision->get_normal()) > 0.1)
{
collider->call_deferred("squash");
/*auto dup_node = collider->duplicate();
auto dup = Object::cast_to<KinematicBody2D>(dup_node);
auto dup_pos = dup->get_position();
dup_pos.x += 24;
dup->set_position(dup_pos);
auto enemies = get_tree()->get_root()->get_node("Main")->find_node("Enemies", true, false);
if (enemies != nullptr) {
enemies->add_child(dup);
}
else
{
WARN_PRINT("Enemies not found!");
dup->queue_free();
}*/
velocity.y = -get_bounce_force();
}
else if (collider->is_in_group("enemy") && (collider->is_in_group("rideable") && Vector2::DOWN.dot(collision->get_normal()) > 0.1))
{
_on_player_touched();
}
else if (collider->is_in_group("enemy") && !collider->is_in_group("rideable"))
{
_on_player_touched();
}
}
// Clamp the player's position inside the camera's limits // Clamp the player's position inside the camera's limits
auto camera = get_node<Camera2D>("Camera2D"); auto camera = get_node<Camera2D>("Camera2D");
auto position = get_global_position(); auto position = get_global_position();
@ -176,6 +226,16 @@ float Player::get_jump_force()
return this->jump_force; return this->jump_force;
} }
void Player::set_bounce_force(float bounce_force)
{
this->bounce_force = bounce_force;
}
float Player::get_bounce_force()
{
return this->bounce_force;
}
void Player::set_gravity(float gravity) void Player::set_gravity(float gravity)
{ {
this->gravity = gravity; this->gravity = gravity;
@ -215,3 +275,12 @@ Vector2 Player::get_velocity()
{ {
return this->velocity; return this->velocity;
} }
void Player::_on_player_touched()
{
auto error = get_tree()->change_scene("res://Main.tscn");
if (error != Error::OK)
{
ERR_PRINT(String().num((int) error) + " Could not load scene!");
}
}

View File

@ -31,6 +31,11 @@ namespace godot
* *
*/ */
const float jump_force = 300.0; const float jump_force = 300.0;
/**
* @brief The default bounce force applied when bouncing on something.
*
*/
const float bounce_force = 200.0;
/** /**
* @brief The default gravity applied to the player. * @brief The default gravity applied to the player.
* *
@ -93,6 +98,11 @@ namespace godot
* *
*/ */
float jump_force; float jump_force;
/**
* @brief The force applied to the player when bouncing off something.
*
*/
float bounce_force;
/** /**
* @brief The gravity applied to the player. * @brief The gravity applied to the player.
* *
@ -194,6 +204,20 @@ namespace godot
*/ */
float get_jump_force(); float get_jump_force();
/**
* @brief Set the bounce force object.
*
* @param[in] bounce_force The new force applied to the player to make him bounce.
*/
void set_bounce_force(float bounce_force);
/**
* @brief Get the bounce force object.
*
* @return float The current force being applied to the player.
*/
float get_bounce_force();
/** /**
* @brief Set the gravity object. * @brief Set the gravity object.
* *
@ -250,6 +274,8 @@ namespace godot
* @return Vector2 Returns the velocity of the player. * @return Vector2 Returns the velocity of the player.
*/ */
Vector2 get_velocity(); Vector2 get_velocity();
void _on_player_touched();
}; };
} }
} }