Ship.gd 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. extends Node2D
  2. onready var MAIN = find_parent("MAIN")
  3. onready var screen_diagonal = get_viewport().size.length()
  4. export var predictor_curve : Curve
  5. var speed = Vector2.ZERO
  6. var MIN_RADIUS = 0.1
  7. var accel_engine = Vector2.ZERO
  8. var accel = Vector2.ZERO
  9. var thrust_scale = 3200
  10. var mouse_active = false
  11. var gravity_constant = 1100.0
  12. var predictor_steps = 400
  13. var predictor_max_delta = 0.06 #seconds
  14. var predictor
  15. var time_flows = false
  16. var gameover = false
  17. var fuel = 1.0
  18. var fuel_usage = 0.17
  19. # Declare member variables here. Examples:
  20. # var a = 2
  21. # var b = "text"
  22. # Called when the node enters the scene tree for the first time.
  23. func _ready():
  24. create_predictor()
  25. $CAMERA.hole.visible = true
  26. $CAMERA/Zshifter/point_here.visible = true
  27. pass # Replace with function body.
  28. func _process(delta):
  29. if gameover and Input.is_mouse_button_pressed(1) and $CAMERA.zoom_target == 1.0:
  30. get_tree().change_scene("res://MAIN.tscn")
  31. var normalised_mousepos = get_viewport().get_mouse_position() - get_viewport().size/2.0
  32. normalised_mousepos /= screen_diagonal
  33. if normalised_mousepos.length() < MIN_RADIUS and not gameover:
  34. if not mouse_active:
  35. MAIN.on_mouse_activate()
  36. mouse_active = true
  37. time_flows = true
  38. $CAMERA.hole.visible = false
  39. $CAMERA.idle_circle.visible = true
  40. $CAMERA/Zshifter/point_here.visible = false
  41. # print(normalised_mousepos)
  42. # print('polar')
  43. accel = Vector2.ZERO
  44. if time_flows:
  45. if mouse_active and not normalised_mousepos.length() == 0:
  46. $visual.rotation = cartesian2polar(normalised_mousepos.x,normalised_mousepos.y).y
  47. accel_engine = normalised_mousepos\
  48. * (1/normalised_mousepos.length()) \
  49. * clamp(normalised_mousepos.length()-MIN_RADIUS,0,min(get_viewport().size.x,get_viewport().size.y)) \
  50. * thrust_scale \
  51. * delta
  52. fuel -= accel_engine.length()/thrust_scale * fuel_usage
  53. if fuel <=0:
  54. $CAMERA/Zshifter/stranded.visible = true
  55. game_over()
  56. $visual.material.set_shader_param("full",fuel)
  57. accel += accel_engine
  58. $visual/fire.visible = accel_engine != Vector2.ZERO
  59. var fire_length = accel_engine.length()/thrust_scale*180
  60. #print (fire_length)
  61. $visual/fire.scale.x = 0.1 + fire_length
  62. #print(accel)
  63. var gravity = integrate_gravities()
  64. if typeof(gravity) == TYPE_VECTOR2:
  65. accel += gravity
  66. speed += accel*delta
  67. update_predictor(delta*8.0)
  68. position += speed*delta
  69. else:
  70. $CAMERA/Zshifter/lithobroken.visible = true
  71. game_over()
  72. func game_over():
  73. gameover = true
  74. time_flows = false
  75. $CAMERA.hole.visible = true
  76. $CAMERA.idle_circle.visible = false
  77. find_parent("MAIN").timer.stop()
  78. for p in MAIN.planets:
  79. p.finish_jam()
  80. func create_predictor():
  81. predictor = Line2D.new()
  82. var curve = Curve.new()
  83. curve.min_value = 0.0
  84. curve.max_value = 1.0
  85. curve.bake()
  86. predictor.width_curve = predictor_curve
  87. predictor.width = 4
  88. add_child(predictor)
  89. func update_predictor(delta):
  90. var p_speed = speed
  91. var p_accel = accel - accel_engine
  92. var p_pos = Vector2.ZERO
  93. var points = PoolVector2Array()
  94. # var delta = predictor_max_delta
  95. points.append(p_pos)
  96. for i in range(predictor_steps):
  97. p_pos += p_speed * delta
  98. p_accel = field_in_globalpos(global_position + p_pos)
  99. if typeof(p_accel) != TYPE_VECTOR2:
  100. break
  101. p_speed += p_accel * delta
  102. points.append(p_pos)
  103. predictor.points = points
  104. #print(predictor.points)
  105. func field_in_globalpos(globalpos):
  106. var gravity = Vector2.ZERO
  107. for p in MAIN.planets:
  108. var dist = p.global_position - globalpos
  109. if dist.length() <= p.radius:
  110. return -1
  111. gravity += p.mass*gravity_constant*dist/pow(dist.length(),3)
  112. return gravity
  113. func integrate_gravities():
  114. return field_in_globalpos(global_position)
  115. # Called every frame. 'delta' is the elapsed time since the previous frame.
  116. #func _process(delta):
  117. # pass