player.gd 3.0 KB

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