Advertisement
thatenbykiki

CharEntity.gd (Player/Mob Management)

Apr 13th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends CharacterBody2D
  2. class_name CharEntity
  3.  
  4. @export var is_player : bool
  5. @export_enum("PLAYER", "RED", "GREEN", "BLUE", "YELLOW") var character: String
  6.  
  7. @onready var anim = $AnimationPlayer
  8. @onready var sprite = $Sprite2D
  9. @onready var atkTimer = $AttackTimer
  10.  
  11. var player_speed : float
  12. var mob_speed = player_speed - 10.0
  13. const BASE_SPEED := 75.0
  14.  
  15. var lastMove := Vector2.ZERO
  16.  
  17. var idling := false
  18. var attacking := false
  19. var blocking := false
  20. var hit := false
  21.  
  22. var sprite_idle
  23. var sprite_run
  24. var sprite_attack
  25. var sprite_block
  26. var sprite_hit
  27.  
  28.  
  29. func _ready():
  30.     set_input()
  31.     set_sprites()
  32.     sprite.texture = sprite_idle
  33.     anim.play("IDLE_DOWN")
  34.     atkTimer.timeout.connect(_on_attack_timeout)
  35.  
  36. func _physics_process(delta):
  37.     if is_player:
  38.         inputHandler()
  39.    
  40.     move_and_slide()
  41.     update_anim()
  42.     _debug()
  43.  
  44. func _debug():
  45.     print("Attacking: " + str(attacking))
  46.     print("Blocking: " + str(blocking))
  47.     print("HIT: " + str(hit))
  48.  
  49. func set_sprites():
  50.     if is_player:
  51.         sprite_idle = preload("res://assets/sprites/player/idle.png")
  52.         sprite_run = preload("res://assets/sprites/player/run.png")
  53.         sprite_attack = preload("res://assets/sprites/player/attack1.png")
  54.         sprite_block = preload("res://assets/sprites/player/block.png")
  55.         sprite_hit = preload("res://assets/sprites/player/hit.png")
  56.     elif !is_player:
  57.         if character == "RED":
  58.             sprite_idle = preload("res://assets/sprites/mobs/red/idle.png")
  59.             sprite_run = preload("res://assets/sprites/mobs/red/run.png")
  60.             sprite_attack = preload("res://assets/sprites/mobs/red/attack.png")
  61.             sprite_hit = preload("res://assets/sprites/mobs/red/hit.png")
  62.         if character == "GREEN":
  63.             pass
  64.         if character == "BLUE":
  65.             pass
  66.         if character == "YELLOW":
  67.             pass
  68.  
  69. func set_input():
  70.     if is_player:
  71.         set_process_input(true)
  72.     if !is_player:
  73.         set_process_input(false)
  74.  
  75. func inputHandler():
  76.     set_input()
  77.    
  78.     var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
  79.     velocity = direction * player_speed
  80.    
  81.     if Input.is_action_just_pressed("attack"):
  82.         attacking = true
  83.         attack()
  84.    
  85.     if Input.is_action_pressed("block"):
  86.         blocking = true
  87.         player_speed = 0
  88.     elif Input.is_action_just_released("block"):
  89.         blocking = false
  90.         player_speed = BASE_SPEED
  91.  
  92. func attack():
  93.     sprite.texture = sprite_attack
  94.     atkTimer.start()
  95.     player_speed = 0
  96.     set_process_input(false)
  97.  
  98. func update_anim(): # Thanks to: thefinaldegreee & AnomAlison from GWJ discord!
  99.     var _anim_name : String
  100.     var _face : String
  101.    
  102.     if !attacking && !blocking:
  103.         sprite.texture = sprite_idle
  104.         if velocity.is_zero_approx() && lastMove.y > 0.0:
  105.         # if velocity.y >= 0 && lastMove.is_equal_approx(Vector2(0, 1)):
  106.             _anim_name = "IDLE_DOWN"
  107.             _face = "DOWN"
  108.         elif velocity.is_zero_approx() && lastMove.y < 0.0:
  109.         #elif velocity.y <= 0 && lastMove.is_equal_approx(Vector2(0, -1)):
  110.             _anim_name = "IDLE_UP"
  111.             _face = "UP"
  112.         elif velocity.is_zero_approx() && lastMove.x < 0.0:
  113.         #elif velocity.x <=0 && lastMove.is_equal_approx(Vector2(-1, 0)):
  114.             sprite.flip_h = false
  115.             _anim_name = "IDLE_LEFT-RIGHT"
  116.             _face = "LEFT"
  117.         elif velocity.is_zero_approx() && lastMove.x > 0.0:
  118.         #elif velocity.x >= 0 && lastMove.is_equal_approx(Vector2(1, 0)):
  119.             sprite.flip_h = true
  120.             _anim_name = "IDLE_LEFT-RIGHT"
  121.             _face = "RIGHT"
  122.        
  123.         if velocity.x > 0:
  124.             sprite.texture = sprite_run
  125.             sprite.flip_h = true
  126.             _anim_name = "RUN_LEFT-RIGHT"
  127.         elif velocity.x < 0:
  128.             sprite.texture = sprite_run
  129.             sprite.flip_h = false
  130.             _anim_name = "RUN_LEFT-RIGHT"
  131.         elif velocity.y > 0:
  132.             sprite.texture = sprite_run
  133.             _anim_name = "RUN_DOWN"
  134.         elif velocity.y < 0:
  135.             sprite.texture = sprite_run
  136.             _anim_name = "RUN_UP"
  137.     elif attacking && !blocking:
  138.         sprite.texture = sprite_attack
  139.         if _face == "DOWN":
  140.             _anim_name = "ATTACK_DOWN"
  141.         elif _face == "UP":
  142.             _anim_name = "ATTACK_UP"
  143.         elif _face == "LEFT":
  144.             _anim_name = "ATTACK_LEFT-RIGHT"
  145.         elif _face == "RIGHT":
  146.             _anim_name = "ATTACK_LEFT-RIGHT"
  147.     elif !attacking && blocking:
  148.         sprite.texture = sprite_block
  149.         if _face == "DOWN":
  150.             _anim_name = "BLOCK_DOWN"
  151.         elif _face == "UP":
  152.             _anim_name = "BLOCK_UP"
  153.         elif _face == "LEFT":
  154.             _anim_name = "BLOCK_LEFT-RIGHT"
  155.         elif _face == "RIGHT":
  156.             _anim_name = "BLOCK_LEFT-RIGHT"
  157.     elif hit:
  158.         sprite.texture = sprite_hit
  159.         if _face == "DOWN":
  160.             _anim_name = "HIT_DOWN"
  161.         elif _face == "UP":
  162.             _anim_name = "HIT_UP"
  163.         elif _face == "LEFT":
  164.             _anim_name = "HIT_LEFT-RIGHT"
  165.         elif _face == "RIGHT":
  166.             _anim_name = "HIT_LEFT-RIGHT"
  167.            
  168.        
  169.     anim.play(_anim_name)
  170.            
  171.     lastMove = get_last_motion().normalized()
  172.  
  173. func _on_attack_timeout():
  174.     set_process_input(true)
  175.     player_speed = BASE_SPEED
  176.     attacking = false
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement