projectile.gd 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. extends KinematicBody2D
  2. const PROJECTILE_SPEED = 400
  3. const MOVING_ENEMY = preload("res://Scripts/moving_enemy.gd")
  4. var direction = Vector2(0, 1)
  5. var material = -3
  6. var disabled = false
  7. func _fixed_process(delta):
  8. var motion = direction * PROJECTILE_SPEED * delta
  9. move(motion)
  10. if (has_node("particles")):
  11. var theta = rad2deg(Vector2(0, -1).angle_to(direction))
  12. get_node("particles").set_param(0, theta)
  13. if (is_colliding()):
  14. var collider = get_collider()
  15. if (collider extends TileMap):
  16. var coords = get_pos() / 32 + direction / 4.0
  17. coords.x = int(coords.x)
  18. coords.y = int(coords.y)
  19. var collided_tile = collider.get_cell(coords.x, coords.y)
  20. if (collided_tile > -1):
  21. print("Collision with tile at " + str(coords))
  22. if (collided_tile == material):
  23. disable(true)
  24. collider.set_cell(coords.x, coords.y, -1)
  25. else:
  26. disable(false)
  27. elif (collider extends MOVING_ENEMY):
  28. print("Collision with moving enemy")
  29. if (collider.armor_type == material):
  30. disable(true)
  31. collider.dead = true
  32. else:
  33. disable(false)
  34. if (disabled):
  35. queue_free()
  36. func disable(destroyed=false):
  37. if (destroyed):
  38. get_parent().get_node("player").get_node("mapsounds").play("destroy")
  39. else:
  40. get_parent().get_node("player").get_node("mapsounds").play("nodestroy")
  41. disabled = true