Skip to content

Commit

Permalink
#7 - stats and hitbox setup for player and enemies; also explored oth…
Browse files Browse the repository at this point in the history
…er collision approaches.
  • Loading branch information
dreammelter committed Oct 13, 2023
1 parent b28665b commit cf172be
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 37 deletions.
6 changes: 5 additions & 1 deletion characters/enemy.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ animations = [{
"speed": 20.0
}]

[node name="Enemy" type="CharacterBody2D"]
[node name="Enemy" type="CharacterBody2D" groups=["enemies"]]
collision_layer = 4
collision_mask = 19
script = ExtResource("1_jev7n")
Expand All @@ -36,4 +36,8 @@ animation = &"run"
autoplay = "idle"

[node name="HitBox" type="Area2D" parent="."]
collision_layer = 4
collision_mask = 19
script = ExtResource("2_uq5v4")

[connection signal="body_entered" from="HitBox" to="." method="_on_hit_box_body_entered"]
13 changes: 12 additions & 1 deletion characters/enemy_chicken.tscn
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
[gd_scene load_steps=4 format=3 uid="uid://cmxq3fvd0juja"]
[gd_scene load_steps=5 format=3 uid="uid://cmxq3fvd0juja"]

[ext_resource type="PackedScene" uid="uid://dkvtkwk0egnv2" path="res://characters/enemy.tscn" id="1_1cmhj"]
[ext_resource type="SpriteFrames" uid="uid://f0ldj5tnmpqt" path="res://resources/enemy_chicken_sprite_frames.tres" id="1_fykrh"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_xnib2"]
size = Vector2(29, 33)

[sub_resource type="RectangleShape2D" id="RectangleShape2D_65als"]
size = Vector2(25, 18)

[node name="EnemyChicken" instance=ExtResource("1_1cmhj")]
speed = 100.0
jump_velocity = -350.0

[node name="Collider" parent="." index="0"]
position = Vector2(-0.5, 0.5)
shape = SubResource("RectangleShape2D_xnib2")

[node name="AnimatedSprite" parent="." index="1"]
sprite_frames = ExtResource("1_fykrh")
animation = &"idle"

[node name="HitShape" type="CollisionShape2D" parent="HitBox" index="0"]
position = Vector2(0.5, -7)
shape = SubResource("RectangleShape2D_65als")
debug_color = Color(0.996078, 0.00392157, 0.145098, 0.419608)
12 changes: 10 additions & 2 deletions characters/enemy_plant.tscn
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
[gd_scene load_steps=4 format=3 uid="uid://lmnxq62cee4x"]
[gd_scene load_steps=5 format=3 uid="uid://lmnxq62cee4x"]

[ext_resource type="PackedScene" uid="uid://dkvtkwk0egnv2" path="res://characters/enemy.tscn" id="1_uy3ws"]
[ext_resource type="SpriteFrames" uid="uid://cvbnepfaeffco" path="res://resources/enemy_plant_sprite_frames.tres" id="2_28syg"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_430p7"]
size = Vector2(28, 35)

[sub_resource type="RectangleShape2D" id="RectangleShape2D_8ao8a"]
size = Vector2(28, 16)

[node name="EnemyPlant" instance=ExtResource("1_uy3ws")]

[node name="Collider" parent="." index="0"]
Expand All @@ -14,4 +17,9 @@ shape = SubResource("RectangleShape2D_430p7")

[node name="AnimatedSprite" parent="." index="1"]
sprite_frames = ExtResource("2_28syg")
animation = &"attack"
animation = &"idle"

[node name="HitShape" type="CollisionShape2D" parent="HitBox" index="0"]
position = Vector2(3, -6)
shape = SubResource("RectangleShape2D_8ao8a")
debug_color = Color(0.997589, 0.00548695, 0.146562, 0.42)
10 changes: 9 additions & 1 deletion characters/enemy_shroom.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=42 format=3 uid="uid://cnvx06eam2w51"]
[gd_scene load_steps=43 format=3 uid="uid://cnvx06eam2w51"]

[ext_resource type="PackedScene" uid="uid://dkvtkwk0egnv2" path="res://characters/enemy.tscn" id="1_wdccf"]
[ext_resource type="Texture2D" uid="uid://do1qs7786e7im" path="res://assets/pixel_adv_32x/Enemies/Mushroom/Hit.png" id="2_khmrq"]
Expand Down Expand Up @@ -273,6 +273,9 @@ animations = [{
"speed": 20.0
}]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_f62cl"]
size = Vector2(26, 13)

[node name="EnemyShroom" instance=ExtResource("1_wdccf")]

[node name="Collider" parent="." index="0"]
Expand All @@ -283,3 +286,8 @@ shape = SubResource("RectangleShape2D_gym6e")
sprite_frames = SubResource("SpriteFrames_7yesw")
animation = &"idle"
frame_progress = 0.660378

[node name="HitShape" type="CollisionShape2D" parent="HitBox" index="0"]
position = Vector2(0, 2.5)
shape = SubResource("RectangleShape2D_f62cl")
debug_color = Color(0.997589, 0.00548695, 0.146562, 0.42)
14 changes: 10 additions & 4 deletions characters/enemy_skull.tscn
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
[gd_scene load_steps=4 format=3 uid="uid://bt7rpacsvkjqn"]
[gd_scene load_steps=5 format=3 uid="uid://bt7rpacsvkjqn"]

[ext_resource type="PackedScene" uid="uid://dkvtkwk0egnv2" path="res://characters/enemy.tscn" id="1_i1uds"]
[ext_resource type="SpriteFrames" uid="uid://dvj6m60w6k8qp" path="res://resources/enemy_skull_sprite_frames.tres" id="2_5n5rx"]

[sub_resource type="CircleShape2D" id="CircleShape2D_ldjna"]
radius = 25.0

[sub_resource type="RectangleShape2D" id="RectangleShape2D_uce0v"]
size = Vector2(25, 22)

[node name="EnemySkull" instance=ExtResource("1_i1uds")]

[node name="Collider" parent="." index="0"]
shape = SubResource("CircleShape2D_ldjna")

[node name="AnimatedSprite" parent="." index="1"]
sprite_frames = ExtResource("2_5n5rx")
animation = &"hit"
animation = &"idle"
autoplay = "idle_shield"
frame = 4
frame_progress = 1.0

[node name="HitShape" type="CollisionShape2D" parent="HitBox" index="0"]
position = Vector2(-0.5, -1)
shape = SubResource("RectangleShape2D_uce0v")
debug_color = Color(0.997589, 0.00548695, 0.146562, 0.42)
35 changes: 31 additions & 4 deletions characters/scripts/enemy.gd
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class_name Enemy
extends CharacterBody2D
## Base Enemy class for easy creation and management.

## General physics details
@export var speed := 200.0
@export var jump_velocity := -400.0

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")


Expand All @@ -15,3 +15,30 @@ func _physics_process(delta):
velocity.y += gravity * delta

move_and_slide()
# _handle_collisons()


## basic hit method to be overriden as needed
func hit() -> void:
print("Enemy has been hit!")


## Handle regular collisions - which... isn't consistently working rn.
#func _handle_collisons() -> void:
# for i in get_slide_collision_count():
# var collider = get_slide_collision(i).get_collider()
# if collider.name == "Player":
# collider.hit()


func _on_hit_box_body_entered(body):
# figure out what came in
# determine how to respond
prints("HITBOX encountered", body.name)
if body.name == "Player":
hit()


## Wandering method used for testing
func _wander() -> void:
pass
32 changes: 21 additions & 11 deletions characters/scripts/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class_name Player
extends CharacterBody2D
## Main class for controlling the player character.
##
## Manages movement and related animations.
## Manages movement, collisions, stats, and related animations.

## Emitted when the animated sprite flips its horizontal direction
signal direction_changed(direction: float)
Expand Down Expand Up @@ -49,10 +49,10 @@ func _physics_process(delta: float) -> void:
velocity.y = JUMP_VELOCITY
_jump_count += 1
if _jump_count > 1:
print("PLAYER is double jumping!")
# print("PLAYER is double jumping!")
animated_sprite.play("dbl_jump")
else:
print("PLAYER is jumping!")
# print("PLAYER is jumping!")
animated_sprite.play("jump")

# Get the input direction and handle the movement/deceleration.
Expand All @@ -66,11 +66,12 @@ func _physics_process(delta: float) -> void:
animated_sprite.play("idle")

move_and_slide()

# Handle Collisions
for i in get_slide_collision_count():
var collision = get_slide_collision(i)
_handle_collisions(collision)
_handle_collisions()


## Take damage from collisions with enemies and level hazards
func hit() -> void:
print("We've been hit!")


## Flips the animated sprite and emits a signal when direction changes
Expand All @@ -83,8 +84,17 @@ func _change_anim_direction(direction: float) -> void:
emit_signal("direction_changed", direction)


## Collision Handler - dunno what it returns yet... if anything
func _handle_collisions(collision: KinematicCollision2D) -> void:
print("PLAYER collided with ", collision.get_collider().name)
## Sort through collisions and set off reactions as necessary
func _handle_collisions() -> void:
for i in get_slide_collision_count():
# var collision = get_slide_collision(i)
var collider = get_slide_collision(i).get_collider()

if not collider.is_class("TileMap"):
print("PLAYER collided with ", collider.name)

# if collider.is_in_group("enemies"):
# print("HELLO????")
# collider.hit()

## Adjust candy bar meter + activate super if it's maxed
28 changes: 15 additions & 13 deletions levels/sandbox.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,30 @@ drag_bottom_margin = 0.0
editor_draw_limits = true
editor_draw_drag_margin = true

[node name="Player" parent="." instance=ExtResource("1_tissq")]
position = Vector2(55, -80)

[node name="RemoteTransform" type="RemoteTransform2D" parent="Player"]
remote_path = NodePath("../../Camera")
[node name="Enemies" type="Node2D" parent="."]

[node name="EnemyChicken" parent="." instance=ExtResource("6_yj3sx")]
position = Vector2(155, -81)
[node name="EnemyShroom" parent="Enemies" instance=ExtResource("6_vb4wa")]
position = Vector2(-569, -32)
collision_layer = 2
collision_mask = 1

[node name="EnemyPlant" parent="." instance=ExtResource("5_ovkd0")]
position = Vector2(-179, 91)
[node name="EnemySkull" parent="Enemies" instance=ExtResource("7_8tgvb")]
position = Vector2(-967, 125)
collision_layer = 2
collision_mask = 1

[node name="EnemyShroom" parent="." instance=ExtResource("6_vb4wa")]
position = Vector2(-569, -32)
[node name="EnemyPlant" parent="Enemies" instance=ExtResource("5_ovkd0")]
position = Vector2(-179, 91)
collision_layer = 2
collision_mask = 1

[node name="EnemySkull" parent="." instance=ExtResource("7_8tgvb")]
position = Vector2(-967, 125)
[node name="EnemyChicken" parent="Enemies" instance=ExtResource("6_yj3sx")]
position = Vector2(155, -81)
collision_layer = 2
collision_mask = 1

[node name="Player" parent="." instance=ExtResource("1_tissq")]
position = Vector2(55, -80)

[node name="RemoteTransform" type="RemoteTransform2D" parent="Player"]
remote_path = NodePath("../../Camera")

0 comments on commit cf172be

Please sign in to comment.