ft: player attacking logic
This commit is contained in:
parent
5811658c64
commit
18ce1716e4
|
|
@ -1,12 +1,24 @@
|
||||||
extends CharacterBody2D
|
extends CharacterBody2D
|
||||||
|
|
||||||
|
@export var attack_duration = 0.12 # TODO: finetune
|
||||||
|
@export var attack_cooldown_duration = 0.4
|
||||||
|
|
||||||
|
@onready var attack_area: Area2D = $AttackArea
|
||||||
|
@onready var attack_timer: Timer = $AttackTimer
|
||||||
|
@onready var attack_cooldown_timer: Timer = $AttackCooldownTimer
|
||||||
|
|
||||||
@export var speed = 200
|
@export var speed = 200
|
||||||
var damage = 1
|
var damage = 10
|
||||||
|
var can_attack = true
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
var screen_size = get_viewport_rect().size
|
var screen_size = get_viewport_rect().size
|
||||||
GameManager.init_screen_size(screen_size.x, screen_size.y)
|
GameManager.init_screen_size(screen_size.x, screen_size.y)
|
||||||
|
|
||||||
|
attack_area.monitoring = false # no collision until attacking
|
||||||
|
attack_timer.wait_time = attack_duration
|
||||||
|
attack_cooldown_timer.wait_time = attack_cooldown_duration
|
||||||
|
|
||||||
func _process(delta):
|
func _process(delta):
|
||||||
velocity = Vector2.ZERO
|
velocity = Vector2.ZERO
|
||||||
if Input.is_action_pressed("move_right"):
|
if Input.is_action_pressed("move_right"):
|
||||||
|
|
@ -17,8 +29,35 @@ func _process(delta):
|
||||||
velocity.y += 1
|
velocity.y += 1
|
||||||
if Input.is_action_pressed("move_up"):
|
if Input.is_action_pressed("move_up"):
|
||||||
velocity.y -= 1
|
velocity.y -= 1
|
||||||
|
if Input.is_action_pressed("try_attack"):
|
||||||
|
try_attack()
|
||||||
|
|
||||||
move_and_collide(speed * velocity * delta)
|
move_and_collide(speed * velocity * delta)
|
||||||
#position += speed * velocity * delta
|
#position += speed * velocity * delta
|
||||||
position = GameManager.get_boundaried_position(position)
|
position = GameManager.get_boundaried_position(position)
|
||||||
|
|
||||||
|
func try_attack() -> void:
|
||||||
|
if not can_attack:
|
||||||
|
return
|
||||||
|
attack()
|
||||||
|
|
||||||
|
func attack() -> void:
|
||||||
|
can_attack = false
|
||||||
|
attack_area.monitoring = true
|
||||||
|
attack_timer.start()
|
||||||
|
# TODO: attack animation+sound+etc
|
||||||
|
|
||||||
|
func _on_attack_timeout() -> void:
|
||||||
|
attack_area.monitoring = false
|
||||||
|
attack_cooldown_timer.start()
|
||||||
|
|
||||||
|
func _on_cooldown_timeout() -> void:
|
||||||
|
can_attack = true
|
||||||
|
|
||||||
|
func _on_attack_hit(body: Node2D) -> void:
|
||||||
|
if body.is_in_group("prey") or body.is_in_group("predators"):
|
||||||
|
if body.has_method("handle_damage"):
|
||||||
|
body.handle_damage(damage)
|
||||||
|
elif body.is_in_group("resources"):
|
||||||
|
pass
|
||||||
|
# TODO: resource handling logic
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
[gd_scene load_steps=5 format=3 uid="uid://dxluckxdkpv4f"]
|
[gd_scene load_steps=6 format=3 uid="uid://dxluckxdkpv4f"]
|
||||||
|
|
||||||
[ext_resource type="Script" uid="uid://di7eglnrnqm6i" path="res://molecular/molecular_player.gd" id="1_0ix7k"]
|
[ext_resource type="Script" uid="uid://di7eglnrnqm6i" path="res://molecular/molecular_player.gd" id="1_0ix7k"]
|
||||||
[ext_resource type="Texture2D" uid="uid://cxwvga07sm3yl" path="res://molecular/assets/player-sprite-placeholder-crop.png" id="2_en8op"]
|
[ext_resource type="Texture2D" uid="uid://cxwvga07sm3yl" path="res://molecular/assets/player-sprite-placeholder-crop.png" id="2_en8op"]
|
||||||
|
|
@ -14,6 +14,9 @@ animations = [{
|
||||||
"speed": 5.0
|
"speed": 5.0
|
||||||
}]
|
}]
|
||||||
|
|
||||||
|
[sub_resource type="RectangleShape2D" id="RectangleShape2D_en8op"]
|
||||||
|
size = Vector2(765.4969, 706.5864)
|
||||||
|
|
||||||
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_4flbx"]
|
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_4flbx"]
|
||||||
radius = 191.95984
|
radius = 191.95984
|
||||||
height = 1295.8773
|
height = 1295.8773
|
||||||
|
|
@ -27,7 +30,27 @@ visibility_layer = 2
|
||||||
scale = Vector2(0.5, 0.5)
|
scale = Vector2(0.5, 0.5)
|
||||||
sprite_frames = SubResource("SpriteFrames_onrkg")
|
sprite_frames = SubResource("SpriteFrames_onrkg")
|
||||||
|
|
||||||
|
[node name="AttackArea" type="Area2D" parent="."]
|
||||||
|
position = Vector2(0, 56)
|
||||||
|
rotation = -1.5732701
|
||||||
|
collision_mask = 2
|
||||||
|
|
||||||
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="AttackArea"]
|
||||||
|
position = Vector2(29.579561, 730.73236)
|
||||||
|
shape = SubResource("RectangleShape2D_en8op")
|
||||||
|
debug_color = Color(0.80813414, 0.3957308, 0.3356335, 0.41960785)
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="." groups=["player"]]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="." groups=["player"]]
|
||||||
position = Vector2(0, 56)
|
position = Vector2(0, 56)
|
||||||
rotation = -1.5732701
|
rotation = -1.5732701
|
||||||
shape = SubResource("CapsuleShape2D_4flbx")
|
shape = SubResource("CapsuleShape2D_4flbx")
|
||||||
|
|
||||||
|
[node name="AttackTimer" type="Timer" parent="."]
|
||||||
|
one_shot = true
|
||||||
|
|
||||||
|
[node name="AttackCooldownTimer" type="Timer" parent="."]
|
||||||
|
one_shot = true
|
||||||
|
|
||||||
|
[connection signal="body_entered" from="AttackArea" to="." method="_on_attack_hit"]
|
||||||
|
[connection signal="timeout" from="AttackTimer" to="." method="_on_attack_timeout"]
|
||||||
|
[connection signal="timeout" from="AttackCooldownTimer" to="." method="_on_cooldown_timeout"]
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
[gd_scene load_steps=13 format=4 uid="uid://b55w56d4twno1"]
|
[gd_scene load_steps=14 format=4 uid="uid://b55w56d4twno1"]
|
||||||
|
|
||||||
[ext_resource type="Texture2D" uid="uid://bto1pnycvianp" path="res://molecular/assets/background/bg.png" id="3_a5cls"]
|
[ext_resource type="Texture2D" uid="uid://bto1pnycvianp" path="res://molecular/assets/background/bg.png" id="3_a5cls"]
|
||||||
[ext_resource type="PackedScene" uid="uid://dxluckxdkpv4f" path="res://molecular/molecular_player.tscn" id="3_b1jr0"]
|
[ext_resource type="PackedScene" uid="uid://dxluckxdkpv4f" path="res://molecular/molecular_player.tscn" id="3_b1jr0"]
|
||||||
|
|
@ -111,6 +111,6 @@ script = ExtResource("4_mys4o")
|
||||||
script = ExtResource("5_cthuy")
|
script = ExtResource("5_cthuy")
|
||||||
cam = NodePath("../player/Camera2D")
|
cam = NodePath("../player/Camera2D")
|
||||||
scene = ExtResource("6_a5cls")
|
scene = ExtResource("6_a5cls")
|
||||||
minCount = 500
|
minCount = 100
|
||||||
maxCount = 1000
|
maxCount = 300
|
||||||
metadata/_custom_type_script = "uid://coetidfssb80w"
|
metadata/_custom_type_script = "uid://coetidfssb80w"
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,9 @@ var mirrorSprite3: Node2D
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
# Called when the node enters the scene tree for the first time.
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
$AnimatedSprite2D.animation = "Healthy"
|
health = maxHealth
|
||||||
$AnimatedSprite2D.play()
|
sprite.animation = "Healthy"
|
||||||
|
sprite.play()
|
||||||
|
|
||||||
mirrorSprite1 = sprite.duplicate()
|
mirrorSprite1 = sprite.duplicate()
|
||||||
mirrorSprite2 = sprite.duplicate()
|
mirrorSprite2 = sprite.duplicate()
|
||||||
|
|
@ -28,7 +29,8 @@ func _process(delta: float) -> void:
|
||||||
_handle_wrapping()
|
_handle_wrapping()
|
||||||
|
|
||||||
func _physics_process(delta: float) -> void:
|
func _physics_process(delta: float) -> void:
|
||||||
self.move(Vector3(randfn(0, 1), randfn(0, 1), 0))
|
#self.move(Vector3(randfn(0, 1), randfn(0, 1), 0))
|
||||||
|
pass
|
||||||
|
|
||||||
func move(motion: Vector3) -> void:
|
func move(motion: Vector3) -> void:
|
||||||
move_and_collide(Vector2(motion.x, motion.y)) # Moves along the given vector
|
move_and_collide(Vector2(motion.x, motion.y)) # Moves along the given vector
|
||||||
|
|
@ -36,26 +38,20 @@ func move(motion: Vector3) -> void:
|
||||||
# Apply boundary to new position
|
# Apply boundary to new position
|
||||||
position = GameManager.get_boundaried_position(position)
|
position = GameManager.get_boundaried_position(position)
|
||||||
|
|
||||||
|
func handle_damage(dmg: int) -> void:
|
||||||
|
health = max(0, health-dmg)
|
||||||
|
if health == 0:
|
||||||
|
die()
|
||||||
|
if health < maxHealth:
|
||||||
|
become_injured()
|
||||||
|
|
||||||
func die() -> void:
|
func die() -> void:
|
||||||
sprite.play("Dying")
|
sprite.play("Dying")
|
||||||
super.die()
|
super.die()
|
||||||
|
|
||||||
|
func become_injured() -> void:
|
||||||
func _on_area_2d_body_entered(body: Node2D) -> void:
|
sprite.animation = "Injured"
|
||||||
# TODO: collision with other entities
|
sprite.play()
|
||||||
# check if colision with player
|
|
||||||
if body.is_in_group("player"):
|
|
||||||
handle_collision_player(body)
|
|
||||||
|
|
||||||
|
|
||||||
# Function to handle collision logic
|
|
||||||
func handle_collision_player(player):
|
|
||||||
self.health -= player.damage
|
|
||||||
if sprite:
|
|
||||||
if self.health <= 0:
|
|
||||||
self.die()
|
|
||||||
else:
|
|
||||||
sprite.play("Injured")
|
|
||||||
|
|
||||||
# FIXME: Doesn't work with injured sprite
|
# FIXME: Doesn't work with injured sprite
|
||||||
# Mirroring table:
|
# Mirroring table:
|
||||||
|
|
|
||||||
|
|
@ -45,9 +45,8 @@ animations = [{
|
||||||
"speed": 1.0
|
"speed": 1.0
|
||||||
}]
|
}]
|
||||||
|
|
||||||
[node name="NucleotidePrey" instance=ExtResource("1_qvulj")]
|
[node name="NucleotidePrey" groups=["prey"] instance=ExtResource("1_qvulj")]
|
||||||
collision_layer = 2
|
collision_layer = 2
|
||||||
collision_mask = 3
|
|
||||||
motion_mode = 1
|
motion_mode = 1
|
||||||
script = ExtResource("2_0227s")
|
script = ExtResource("2_0227s")
|
||||||
maxHealth = 20
|
maxHealth = 20
|
||||||
|
|
@ -57,9 +56,3 @@ scale = Vector2(0.1, 0.1)
|
||||||
sprite_frames = SubResource("SpriteFrames_66x8p")
|
sprite_frames = SubResource("SpriteFrames_66x8p")
|
||||||
animation = &"Dying"
|
animation = &"Dying"
|
||||||
frame_progress = 0.60472965
|
frame_progress = 0.60472965
|
||||||
|
|
||||||
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Area2D" index="0"]
|
|
||||||
position = Vector2(7.215866, -0.5034294)
|
|
||||||
polygon = PackedVector2Array(-8.831272, -2.3390446, -5.7760534, -2.3993049, -4.0230684, 0.41277456, -5.8890305, 2.9948444, -8.862037, 2.9326901, -10.268076, 0.28495264)
|
|
||||||
|
|
||||||
[connection signal="body_entered" from="Area2D" to="." method="_on_area_2d_body_entered"]
|
|
||||||
|
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
extends Node
|
|
||||||
|
|
||||||
var preyScene = preload("res://molecular/nucleotide_prey.tscn")
|
|
||||||
#var predatorScene = preload("res://Predator.tscn")
|
|
||||||
var score
|
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
|
||||||
func _ready() -> void:
|
|
||||||
for i in range(0, 100, 10):
|
|
||||||
spawn_creature(preyScene, Vector2(i, i))
|
|
||||||
spawn_random()
|
|
||||||
spawn_random()
|
|
||||||
spawn_random()
|
|
||||||
spawn_random()
|
|
||||||
|
|
||||||
|
|
||||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
||||||
func _process(delta: float) -> void:
|
|
||||||
pass
|
|
||||||
|
|
||||||
func spawn_random() -> void:
|
|
||||||
spawn_creature(preyScene, Vector2(randfn(100, 100), randfn(100, 100)))
|
|
||||||
|
|
||||||
func spawn_creature(scene, position: Vector2) -> void:
|
|
||||||
var instance = scene.instantiate()
|
|
||||||
instance.position = position
|
|
||||||
add_child(instance)
|
|
||||||
|
|
||||||
|
|
@ -22,6 +22,7 @@ GameManager="*res://game_manager.gd"
|
||||||
[global_group]
|
[global_group]
|
||||||
|
|
||||||
player="All scenes that constitute players should be added here."
|
player="All scenes that constitute players should be added here."
|
||||||
|
prey="any passive killable entities belong in this group"
|
||||||
|
|
||||||
[input]
|
[input]
|
||||||
|
|
||||||
|
|
@ -45,6 +46,11 @@ move_down={
|
||||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null)
|
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
try_attack={
|
||||||
|
"deadzone": 0.2,
|
||||||
|
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
[layer_names]
|
[layer_names]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,4 +5,6 @@
|
||||||
[node name="NPC" type="CharacterBody2D"]
|
[node name="NPC" type="CharacterBody2D"]
|
||||||
script = ExtResource("1_ucjfp")
|
script = ExtResource("1_ucjfp")
|
||||||
|
|
||||||
[node name="Area2D" type="Area2D" parent="."]
|
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
|
||||||
|
position = Vector2(7.215866, -0.5034294)
|
||||||
|
polygon = PackedVector2Array(-8.831272, -2.3390446, -5.7760534, -2.3993049, -4.0230684, 0.41277456, -5.8890305, 2.9948444, -8.862037, 2.9326901, -10.268076, 0.28495264)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue