I need a help to implement my idea into game

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By amaou310

Hello community. I am trying to implement following event in my 2d platformer project. However, with lack of knowledge and experience in game making, I am having hard time to implement this. I would appreciate if anyone could give me a solution or an idea. Thank you in advance.

① Player collide enemy
②-① play “zombie biting player” animation
②-② Player become invisible and cannot move
②-③ trigger event “press spacebar repeatedly to fill the progress bar”
③-① if progress bar is filled, Player bounce off from enemy
③-② if player doesn’t press spacebar and if animation end (or after certain amount of time), player bounce off from enemy and lose certain amount of health

Both player and enemy are kinematicbody2D.

In player script:

If player collide with enemy (enter area signal )
>player.visible = false (or queue_free)
>can_move = false
>trigger event "press spacebar repeatedly to fill the progress bar"

In enemy script:

if enemy collide with player
> animation.play ("biting")

for
1
and
2 1

This PLayer scene script

#I assume you have a Area2D attached in the player

func _on_Area2D_body_entered(body):
# This will cause you problems in the future.
# 
  if body.name == "enemy":
	  body.zombie_biting_player()

# OR 
## learn groups . "Godot Group"
# "Add group" to enemy
  if body.is_in_group("enemy"):
	  body.zombie_biting_player()
pass

Zombie scene script

func zombie_biting_player():
  anim.play("biting")
pass

ramazan | 2022-07-04 08:41

:bust_in_silhouette: Reply From: Inces

You didn’t specify what exactly is hard for You to implement. I guess it is a question about general code structure for this.

First of all You need to design state machine for player. There must be a normal state, in which player moves normally according to input, and being_bited state, when it becomes invisible, does not listen to movement input, but reacts to spacebar button.

Change of state can be detected by setget function. Whenever state is changed to being_bited You can emit a signal for UI to show and activate a progress bar.
You can also set players visibility here.

in players script input() - whenever a space is pressed and a state is being_bited - add up your progress value and emit a signal for UI to visualize this value on progress bar. When value is 100% call your bounce_off function, reset progress value and return to normal state, hiding/freeing progress_bar at the same time

when collision between zombie and player occurs ( on collision signal ) - force being_bited state on player, play animation for zombie.
When zombie animation ends (also use signal) and it is still colliding with player - emit a signal for player to trigger lose of hp and his bounce_off function.

Zombie also needs a state machine - one state for following the player, and another one for standing still in biting animation

This is quite simple for one zombie at a time. If You will have a situations of multiple zombies rushing at player, You need to design a way your player will behave. I believe You will want to be bited by only one zombie at a time. If yes, You can prevent collision bahavior by preceding it with line :

if player.state == Player.being_bited:
     return

Thank you for an answer Inces.
Yes I was looking for a general code structure for my idea.
My player current can move right and left and jump and shooot like in the gif.
And now i know what I need to do to implement my idea. First is to make state machine(would be very hard for me but i will try).

amaou310 | 2022-07-06 08:19