|
| 1 | +extends Node2D |
| 2 | + |
| 3 | +signal chip_spawned(chip) |
| 4 | + |
| 5 | +const END_MESSAGE := "%s won" |
| 6 | +const CHIP_SCRIPT := preload("res://games/asecondguy_connect/chip.gd") |
| 7 | + |
| 8 | +var player_names := [] |
| 9 | +var player_colors := [] |
| 10 | +var fallen_chips := [] |
| 11 | + |
| 12 | +var _last_chip: CHIP_SCRIPT |
| 13 | +var _end_condition := 0 |
| 14 | +var _chips_played := 0 |
| 15 | +var _win_chip_pos := [] |
| 16 | + |
| 17 | +onready var _play_area := $PlayArea |
| 18 | +onready var _grid := $Grid |
| 19 | +onready var _chips := $Chips |
| 20 | +onready var _spawners := [$Spawner1, $Spawner2] |
| 21 | +onready var _info_label := $UI/PanelContainer/InfoLabel |
| 22 | + |
| 23 | + |
| 24 | +func _ready(): |
| 25 | + randomize() |
| 26 | + # setup the code representation of all played chips |
| 27 | + for _x in range(_grid.grid_size.x): |
| 28 | + var tmp := [] |
| 29 | + for _y in range(_grid.grid_size.y): |
| 30 | + tmp.push_back(null) |
| 31 | + fallen_chips.push_back(tmp) |
| 32 | + |
| 33 | + |
| 34 | +func start(): |
| 35 | + _spawn_chip() |
| 36 | + |
| 37 | + |
| 38 | +func _empty(): |
| 39 | + $Bounds/Bottom.set_deferred("disabled", true) |
| 40 | + for c in _chips.get_children(): |
| 41 | + # set all chips back to rigid mode |
| 42 | + c.set_deferred("mode", 0) |
| 43 | + if !_win_chip_pos.empty(): |
| 44 | + var line: Line2D = preload("res://games/asecondguy_connect/win_line.tscn").instance() |
| 45 | + line.grid_points = _win_chip_pos |
| 46 | + line.default_color = player_colors[_last_chip.player_id] |
| 47 | + _grid.add_child(line) |
| 48 | + if _end_condition == 0: |
| 49 | + _info_label.start("Draw") |
| 50 | + else: |
| 51 | + _info_label.start(END_MESSAGE % player_names[_end_condition - 1]) |
| 52 | + |
| 53 | + |
| 54 | +func _on_PlayArea_body_exited(_body): |
| 55 | + if _end_condition == -1: |
| 56 | + return |
| 57 | + if _play_area.get_overlapping_bodies().size() == 0: |
| 58 | + if _end_condition == 0: |
| 59 | + GameManager.end_game("Draw") |
| 60 | + else: |
| 61 | + GameManager.end_game(END_MESSAGE % player_names[_end_condition - 1]) |
| 62 | + |
| 63 | + |
| 64 | +func _on_chip_sleep(chip: RigidBody2D): |
| 65 | + if !chip.sleeping: |
| 66 | + return |
| 67 | + _last_chip = chip |
| 68 | + var grid_pos: Vector2 = _grid.global_to_grid_pos(chip.position) |
| 69 | + if _grid.is_in_grid(grid_pos): |
| 70 | + chip.set_deferred("mode", chip.MODE_STATIC) |
| 71 | + _chips_played += 1 |
| 72 | + fallen_chips[grid_pos.x][grid_pos.y] = chip.player_id |
| 73 | + _check_game_end_from_chip(grid_pos, chip.player_id) |
| 74 | + # max number of chips is 42 |
| 75 | + if _chips_played == 42 or _end_condition != 0: |
| 76 | + _empty() |
| 77 | + else: |
| 78 | + # [1, 0][last_id] ==> 0 -> 1, 1 -> 0 |
| 79 | + _spawn_chip([1, 0][chip.player_id]) |
| 80 | + |
| 81 | + |
| 82 | +func _spawn_chip(player_id := 0): |
| 83 | + var chip: RigidBody2D = preload("res://games/asecondguy_connect/chip.tscn").instance() |
| 84 | + chip.player_id = player_id |
| 85 | + chip.color = player_colors[player_id] |
| 86 | + chip.global_position = ( |
| 87 | + _spawners[player_id].global_position |
| 88 | + + Vector2(rand_range(-10, 10), rand_range(-10, 10)) |
| 89 | + ) |
| 90 | + if chip.connect("sleeping_state_changed", self, "_on_chip_sleep", [chip]) != OK: |
| 91 | + GameManager.end_game("A fatal error occured.") |
| 92 | + _chips.call_deferred("add_child", chip) |
| 93 | + _info_label.start(player_names[player_id] + "'s turn") |
| 94 | + emit_signal("chip_spawned", chip) |
| 95 | + |
| 96 | + |
| 97 | +# checks the chip and updates _end_condition |
| 98 | +func _check_game_end_from_chip(at: Vector2, player_id): |
| 99 | + # check all 4 valid axis |
| 100 | + for axis in [Vector2(1, 0), Vector2(0, 1), Vector2(1, 1), Vector2(-1, 1)]: |
| 101 | + # check both directions of the axis and add them together |
| 102 | + var dir1: int = mesure_chip_run(at, axis, player_id) |
| 103 | + var dir2: int = mesure_chip_run(at, axis * -1, player_id) |
| 104 | + var count := 1 + dir1 + dir2 |
| 105 | + # if the row is at least 4 long the end condition changes |
| 106 | + if count >= 4: |
| 107 | + _end_condition = player_id + 1 |
| 108 | + _win_chip_pos.push_back(at) |
| 109 | + for i in range(dir1): |
| 110 | + _win_chip_pos.push_back(at + axis * (i + 1)) |
| 111 | + _win_chip_pos.invert() |
| 112 | + for i in range(dir2): |
| 113 | + _win_chip_pos.push_back(at - axis * (i + 1)) |
| 114 | + return |
| 115 | + |
| 116 | + |
| 117 | +# assumes origin is a valid chip and has the belongs to the player of player_id |
| 118 | +# this doesn't count the first chip |
| 119 | +# it goes only in one direction (dir) untill it leaves the grid |
| 120 | +func mesure_chip_run(origin: Vector2, dir: Vector2, player_id: int) -> int: |
| 121 | + var pos := origin + dir |
| 122 | + # next position is outside the grid -> run ends |
| 123 | + if !_grid.is_in_grid(pos): |
| 124 | + return 0 |
| 125 | + var chip = fallen_chips[pos.x][pos.y] |
| 126 | + # the next chip belongs to another player -> run ends |
| 127 | + if chip != player_id: |
| 128 | + return 0 |
| 129 | + # the next chip belongs to this run |
| 130 | + # so the total run lenght is the run lengh from the next chip +1 (this chip) |
| 131 | + return mesure_chip_run(pos, dir, player_id) + 1 |
| 132 | + |
| 133 | + |
| 134 | +func _on_PlayArea_tree_exiting(): |
| 135 | + _end_condition = -1 |
0 commit comments