123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- extends Node2D
- onready var cam = $MainCamera
- export var dist_max = 900
- export var zoom_rate = 0.001
- var levelNames = ["res://Levels/Lab.tscn", "res://Levels/Square.tscn"]
- var levelScenes = []
- var levelCurrentNode
- var levelIndex = -1
- var playerScene = preload("res://Player.tscn")
- func _ready():
- for k in range(levelNames.size()):
- levelScenes.append(null)
- func _process(_d):
- move_camera()
- func _unhandled_input(event):
- if event.is_action("ui_accept") and event.is_pressed():
- if get_tree().has_group("living"):
- var living = get_tree().get_nodes_in_group("living")
- if !living.empty():
- return
- start()
- func start():
- # Clear if required
- if (get_tree().has_group("players")):
- var players = get_tree().get_nodes_in_group("players")
- for p in players:
- p.queue_free()
- if (levelCurrentNode):
- levelCurrentNode.queue_free()
- # Select level
- levelIndex += 1
- if (levelIndex >= levelScenes.size()):
- levelIndex = 0
- if !levelScenes[levelIndex]:
- levelScenes[levelIndex] = load(levelNames[levelIndex])
- # Load level
- levelCurrentNode = levelScenes[levelIndex].instance()
- add_child(levelCurrentNode)
- # Create players
- var player
- # Player 1
- player = playerScene.instance()
- player.grid = levelCurrentNode
- player.turn_left_action = "player_1_turn_left"
- player.turn_right_action = "player_1_turn_right"
- player.position = Vector2(4 * 64 + 32, 8 * 64 + 32)
- add_child(player)
- # Player 2
- player = playerScene.instance()
- player.grid = levelCurrentNode
- player.turn_left_action = "player_2_turn_left"
- player.turn_right_action = "player_2_turn_right"
- player.position = Vector2(6 * 64 + 32, 8 * 64 + 32)
- add_child(player)
- # TODO Timer
- get_tree().call_group("players", "_on_game_start")
- func move_camera():
- if !get_tree().has_group("players"):
- return
- var living
- if get_tree().has_group("living"):
- living = get_tree().get_nodes_in_group("living")
- if !living or living.size() == 0:
- # Show every players
- var players = get_tree().get_nodes_in_group("players")
- center_on(players)
- else:
- # Show remaining players only
- center_on(living)
- func center_on(players):
- var position_accumulator = Vector2(0.0, 0.0)
- for p in players:
- position_accumulator += p.position
- cam.position = position_accumulator / players.size()
- var dist = 0
- for i in range(players.size() - 1):
- for j in range(i + 1, players.size()):
- dist = max(dist, players[i].position.distance_to(players[j].position))
- # Extend camera zoom if liners are far from each other
- var zoom = 1.0
- if dist > dist_max:
- zoom += (dist - dist_max) * zoom_rate
- cam.zoom = Vector2(zoom, zoom)
|