player.gd 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. extends KinematicBody2D
  2. const WALK_SPEED = 150
  3. const PROJECTILE_BUFFER = 24
  4. const COMMANDS = "Q - Reset\nF - Cast spell\nG - Next spell"
  5. const FIREBALL = preload("res://Scenes/fireball.xscn")
  6. const LIGHTNING = preload("res://Scenes/lightning.xscn")
  7. var anim = ""
  8. var facing = 1
  9. var velocity = Vector2()
  10. var stamina = 100
  11. var dead = false
  12. var selected_spell = -1
  13. var spellbook = [{"name" : "fireball", "cost" : 10},
  14. {"name" : "lightning", "cost" : 20}]
  15. func _input(event):
  16. var valid = event.is_pressed() && !event.is_echo()
  17. var level = get_node("/root/global").level
  18. if event.is_action("reset") && valid:
  19. get_node("/root/global").goto_scene(get_parent().get_filename())
  20. elif dead:
  21. pass
  22. elif event.is_action("next_spell") && valid:
  23. next_spell()
  24. elif event.is_action("cast") && valid:
  25. cast()
  26. func next_spell():
  27. selected_spell = (selected_spell + 1) % get_node("/root/global").spell_level
  28. var s = spellbook[selected_spell]
  29. get_parent().get_node("commands").get_node("spell_info").set_text(s["name"] + " - " + str(s["cost"]))
  30. func cast():
  31. var s = spellbook[selected_spell]
  32. stamina = stamina - s["cost"]
  33. if s["name"] == "fireball":
  34. shoot_projectile(FIREBALL)
  35. elif s["name"] == "lightning":
  36. shoot_projectile(LIGHTNING)
  37. func shoot_projectile(projectile_type):
  38. var p = projectile_type.instance()
  39. get_parent().add_child(p)
  40. if (facing == 0):
  41. p.direction = Vector2(0, -1)
  42. p.set_pos(get_pos() + Vector2(0, -PROJECTILE_BUFFER))
  43. elif (facing == 1):
  44. p.direction = Vector2(0, 1)
  45. p.set_pos(get_pos() + Vector2(0, PROJECTILE_BUFFER))
  46. elif (facing == 2):
  47. p.direction = Vector2(-1, 0)
  48. p.set_pos(get_pos() + Vector2(-PROJECTILE_BUFFER, 0))
  49. else:
  50. p.direction = Vector2(1, 0)
  51. p.set_pos(get_pos() + Vector2(PROJECTILE_BUFFER, 0))
  52. get_node("playersounds").play("shoot")
  53. func _fixed_process(delta):
  54. var new_anim = anim
  55. velocity = Vector2(0, 0)
  56. if (Input.is_action_pressed("ui_up")):
  57. new_anim = "up"
  58. facing = 0
  59. velocity.y = -WALK_SPEED
  60. elif (Input.is_action_pressed("ui_down")):
  61. new_anim = "down"
  62. facing = 1
  63. velocity.y = WALK_SPEED
  64. elif (Input.is_action_pressed("ui_left")):
  65. new_anim = "left"
  66. facing = 2
  67. velocity.x = -WALK_SPEED
  68. elif (Input.is_action_pressed("ui_right")):
  69. new_anim = "right"
  70. facing = 3
  71. velocity.x = WALK_SPEED
  72. get_parent().get_node("stamina").get_node("value").set_text(str(stamina) + " / 100")
  73. if (stamina <= 0):
  74. get_parent().get_node("losepanel").show()
  75. hide()
  76. clear_shapes()
  77. dead = true
  78. if (stamina < 0):
  79. stamina = 0
  80. var motion = velocity * delta
  81. move(motion)
  82. if (is_colliding()):
  83. var n = get_collision_normal()
  84. motion = n.slide(motion)
  85. velocity = n.slide(velocity)
  86. move(motion)
  87. if (new_anim != anim):
  88. anim = new_anim
  89. get_node("anim").play(anim)
  90. func _ready():
  91. set_fixed_process(true)
  92. set_process_input(true)
  93. get_parent().get_node("commands").get_node("text").set_text(COMMANDS)
  94. next_spell()