TrackPiece.gd 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. class_name TrackPiece
  2. extends Node2D
  3. var editing := false
  4. var mouse_in_areas := 0
  5. var pieces := []
  6. var idx := 0
  7. var starting_direction := Vector2i.ZERO
  8. var connecting_direction := Vector2i.UP
  9. const SPRITE_COORDS := {
  10. Vector2i.ZERO: Vector2(112, 16),
  11. Vector2i.RIGHT: Vector2(0, 0),
  12. Vector2i.LEFT: Vector2(16, 0),
  13. Vector2i.DOWN: Vector2(32, 0),
  14. Vector2i.UP: Vector2(48, 0),
  15. Vector2i(1, 1): Vector2(64, 0),
  16. Vector2i(-1, 1): Vector2(80, 0),
  17. Vector2i(-1, -1): Vector2(96, 0),
  18. Vector2i(1, -1): Vector2(112, 0),
  19. }
  20. const TRACKS = preload("uid://50hm4xgnw8ks")
  21. const INVISIBLE_TRACKS = preload("uid://barofu3g8jf00")
  22. func _process(_delta: float) -> void:
  23. $PlacePreview.visible = editing
  24. $Start.region_rect.position = SPRITE_COORDS[starting_direction]
  25. $Connect.region_rect.position = SPRITE_COORDS[connecting_direction]
  26. $End.visible = idx == owner.length
  27. $End.frame = int(owner.end_point == 0)
  28. if Input.is_action_pressed("mb_left") and editing and mouse_in_areas > 0:
  29. for i in 8:
  30. if is_mouse_in_area(i):
  31. if Track.DIRECTIONS[i] == starting_direction:
  32. owner.remove_last_piece()
  33. else:
  34. owner.add_piece(Track.DIRECTIONS[i])
  35. func update_direction_textures() -> void:
  36. var texture = TRACKS
  37. if owner.invisible:
  38. texture = INVISIBLE_TRACKS
  39. for i in $PlacePreview.get_children():
  40. i.frame = int(Track.DIRECTIONS[i.get_index()] == starting_direction)
  41. for i in [$Start, $Connect, $End]:
  42. i.texture = texture
  43. func on_mouse_entered(area_idx := 0) -> void:
  44. mouse_in_areas |= (1 << area_idx)
  45. print(mouse_in_areas)
  46. func on_mouse_exited(area_idx := 0) -> void:
  47. mouse_in_areas &= ~(1 << area_idx)
  48. print(mouse_in_areas)
  49. func is_mouse_in_area(area_idx := 0) -> bool:
  50. return mouse_in_areas & (1 << area_idx) != 0