basic 2 player boilerplate

This commit is contained in:
yuki 2025-11-14 14:19:26 -03:00
parent 77c005e5db
commit fe15b62976
2 changed files with 22 additions and 10 deletions

View file

@ -17,8 +17,12 @@ tile_map_data = PackedByteArray("AAADAA0AAwAEAAIAAAADAAwAAwAEAAIAAAADAAsAAwAEAAI
tile_set = ExtResource("1_uu6xs") tile_set = ExtResource("1_uu6xs")
metadata/_edit_lock_ = true metadata/_edit_lock_ = true
[node name="Player" parent="." instance=ExtResource("2_r0du0")] [node name="Player1" parent="." instance=ExtResource("2_r0du0")]
position = Vector2(148, 105) position = Vector2(141, 184)
[node name="Player2" parent="." instance=ExtResource("2_r0du0")]
position = Vector2(154, 97)
id = 2
[node name="Ball" parent="." instance=ExtResource("3_r0du0")] [node name="Ball" parent="." instance=ExtResource("3_r0du0")]
position = Vector2(193, 130) position = Vector2(193, 130)

View file

@ -1,16 +1,20 @@
extends CharacterBody2D extends CharacterBody2D
@export var id: int = 1 @export var id: int = 1
@export var max_speed: float = 150 @export var max_speed: float = 90
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D @onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
var is_hitting: bool = false var is_hitting: bool = false
var was_moving: bool = false var was_moving: bool = false
var anim_dir: String = 'down' var anim_dir: String
var def_dir: String
func _ready() -> void: func _ready() -> void:
sprite.play('down') assert(id == 1 or id == 2, "id ("+str(id)+") is invalid")
anim_dir = 'up' if id == 1 else 'down'
def_dir = 'up' if id == 1 else 'down'
sprite.play(def_dir)
sprite.frame = 1 sprite.frame = 1
sprite.pause() sprite.pause()
@ -28,7 +32,7 @@ func _physics_process(_delta: float) -> void:
elif Input.is_action_pressed("hit_right") and not is_hitting: elif Input.is_action_pressed("hit_right") and not is_hitting:
hit("right") hit("right")
if is_moving: if is_moving and not is_hitting:
if abs(velocity.x) > abs(velocity.y): if abs(velocity.x) > abs(velocity.y):
anim_dir = 'right' if velocity.x > 0 else 'left' anim_dir = 'right' if velocity.x > 0 else 'left'
else: else:
@ -37,7 +41,7 @@ func _physics_process(_delta: float) -> void:
sprite.play(anim_dir) sprite.play(anim_dir)
else: else:
if was_moving and not is_hitting: if was_moving and not is_hitting:
sprite.play('down') sprite.play(def_dir)
sprite.frame = 1 sprite.frame = 1
sprite.pause() sprite.pause()
@ -50,7 +54,7 @@ func get_movement_vector() -> Vector2:
func hit(dir: String) -> void: func hit(dir: String) -> void:
if not is_hitting: if not is_hitting:
sprite.play('up') sprite.play('up' if id == 1 else 'down')
sprite.frame = 1 sprite.frame = 1
sprite.pause() sprite.pause()
@ -58,14 +62,18 @@ func hit(dir: String) -> void:
var hit_node: Area2D = preload("res://scenes/hit/hit.tscn").instantiate() var hit_node: Area2D = preload("res://scenes/hit/hit.tscn").instantiate()
# flip entire node horizontally if spawning left # flip entire node horizontally if spawning left
hit_node.scale = Vector2(-1 if dir == "left" else 1, 1) # flip vertically if player 2
hit_node.scale = Vector2(
-1 if dir == "left" else 1,
1 if id == 1 else -1
)
add_child(hit_node) add_child(hit_node)
# set position according to frame width and height # set position according to frame width and height
@warning_ignore("integer_division") @warning_ignore("integer_division")
hit_node.global_position = global_position + Vector2( hit_node.global_position = global_position + Vector2(
(sprite_texture.get_width()/2)*(-1 if dir == "left" else 1), (sprite_texture.get_width()/2)*(-1 if dir == "left" else 1),
sprite_texture.get_height()*-1 (sprite_texture.get_height()*-1) if id == 1 else 4
) )
hit_node.timer.connect("timeout", _on_hit_end) hit_node.timer.connect("timeout", _on_hit_end)