diff --git a/prefabs/grid/grid.gd b/prefabs/grid/grid.gd index f2dacf4..649d8b9 100644 --- a/prefabs/grid/grid.gd +++ b/prefabs/grid/grid.gd @@ -3,7 +3,6 @@ extends Node2D signal tile_pressed(v) -var tileset_texture_2x: ImageTexture var grid: Dictionary# var Army = preload("res://prefabs/units/army/army.tscn") var Boat = preload("res://prefabs/units/boat/boat.tscn") @@ -113,6 +112,7 @@ func unit_can_move_to(unit, destination): func move_unit(unit, destination): var v = $TileMap.world_to_map(unit.entity.position/tilemap.scale) + var tile = grid[v] var dest_tile = grid[destination] if not unit_can_move_to(unit, destination): return false @@ -120,8 +120,29 @@ func move_unit(unit, destination): grid[destination].units.append(unit) unit.entity.position = destination * total_scale + total_scale*0.5 unit.position = destination + + var conflict = false + for unit2 in dest_tile.units: + if unit2.country != unit.country: + conflict = true + + if conflict: + resolve_conflict(dest_tile.units) + else: + set_country(destination, unit.country) + + if tile.needs_boat: + set_country(v, countries[0]) + return true +func resolve_conflict(armies): + var survivor = armies[randi() % len(armies)] + for army in armies: + if army != survivor: + army.country.remove_unit(army) + set_country(survivor.position, survivor.country) + func remove_unit(unit): var v = $TileMap.world_to_map(unit.entity.position/tilemap.scale) unit.entity.queue_free() @@ -143,6 +164,7 @@ func boat_can_move_to(unit, destination): func move_boat(unit, destination): var v = $TileMap.world_to_map(unit.entity.position/tilemap.scale) + var tile = grid[v] var dest_tile = grid[destination] if not boat_can_move_to(unit, destination): return false @@ -150,6 +172,9 @@ func move_boat(unit, destination): grid[destination].boats.append(unit) unit.entity.position = destination * total_scale + total_scale*0.5 unit.position = destination + + if len(tile.units) == 1 and tile.units[0].country == unit.country: + move_unit(tile.units[0], destination) return true func has_adjacent_factory(v, country): @@ -161,16 +186,6 @@ func has_adjacent_factory(v, country): return false func setup(): - _setup_tileset_texture_2x() - _setup_map() - -func _setup_tileset_texture_2x(): - tileset_texture_2x = ImageTexture.new() - var image = preload("res://assets/tileset.png").get_data() - image.resize(image.get_width()*2, image.get_height()*2, Image.INTERPOLATE_NEAREST) - tileset_texture_2x.create_from_image(image) - -func _setup_map(): var image = preload("res://assets/testmap8x8.png") var data = image.get_data() data.lock() @@ -195,14 +210,6 @@ func _process(delta): if position.x < 8 and position.y < 8: emit_signal("tile_pressed", position) -func _get_atlas_from_tileset(tileset, name, texture): - var res = AtlasTexture.new() - res.atlas = texture - res.region = $TileMap.tile_set.tile_get_region( - $TileMap.tile_set.find_tile_by_name(name) - ) - return res - func _draw(): var scale = $TileMap.scale * $TileMap.cell_size for x in 8: diff --git a/prefabs/units/army/army.gd b/prefabs/units/army/army.gd index ac1c2d2..017a94d 100644 --- a/prefabs/units/army/army.gd +++ b/prefabs/units/army/army.gd @@ -1,3 +1,4 @@ +class_name Army extends Area2D var data#unit data @@ -6,10 +7,8 @@ var country: Country var current_turn_movable = true onready var game = get_tree().current_scene -func _on_Army_input_event(viewport, event, shape_idx): - if event is InputEventMouseButton: - if event.pressed and event.button_index == BUTTON_LEFT: - grid.selected_thing = self +func get_tile_position(): + return grid.tilemap.world_to_map(position/grid.tilemap.scale) func _process(delta): update() @@ -26,30 +25,10 @@ func _process(delta): var dx = abs(floor(pos.x-mouse_pos.x)) var dy = abs(floor(pos.y-mouse_pos.y)) - if dx != dy and (dx == 1 or dy == 1): + if dx != dy and dx <= 1 and dy <= 1: if game.country_turn_id == country.id and current_turn_movable: if grid.move_unit(data, mouse_pos): current_turn_movable = false - - var conflict = false - for unit in grid.grid[mouse_pos].units: - if unit.country != country: - conflict = true - - if conflict: - resolve_conflict(grid.grid[mouse_pos].units) - else: - grid.set_country(mouse_pos, country) - - if grid.grid[pos].needs_boat: - grid.set_country(pos, grid.countries[0]) - -func resolve_conflict(armies): - var survivor = armies[randi() % len(armies)] - for army in armies: - if army != survivor: - army.country.remove_unit(army) - grid.set_country(survivor.position, survivor.country) func _draw(): if grid.selected_thing == self: diff --git a/prefabs/units/army/army.tscn b/prefabs/units/army/army.tscn index 7c7a210..2110cba 100644 --- a/prefabs/units/army/army.tscn +++ b/prefabs/units/army/army.tscn @@ -16,5 +16,3 @@ scale = Vector2( 2, 2 ) texture = ExtResource( 1 ) region_enabled = true region_rect = Rect2( 16, 32, 16, 16 ) - -[connection signal="input_event" from="." to="." method="_on_Army_input_event"] diff --git a/prefabs/units/boat/boat.gd b/prefabs/units/boat/boat.gd index a575a8d..92203a5 100644 --- a/prefabs/units/boat/boat.gd +++ b/prefabs/units/boat/boat.gd @@ -1,3 +1,4 @@ +class_name Boat extends Area2D var data#unit data @@ -6,10 +7,8 @@ var country: Country var current_turn_movable = true onready var game = get_tree().current_scene -func _on_Boat_input_event(viewport, event, shape_idx): - if event is InputEventMouseButton: - if event.pressed and event.button_index == BUTTON_LEFT: - grid.selected_thing = self +func get_tile_position(): + return grid.tilemap.world_to_map(position/grid.tilemap.scale) func _process(delta): update() @@ -20,13 +19,13 @@ func _process(delta): if not grid.selected_thing == self: return if Input.is_action_just_pressed("move"): - var pos = grid.tilemap.world_to_map(position/grid.tilemap.scale) + var pos = get_tile_position() var mouse_pos = grid.tilemap.world_to_map(get_global_mouse_position()/grid.tilemap.scale) var dx = abs(floor(pos.x-mouse_pos.x)) var dy = abs(floor(pos.y-mouse_pos.y)) - if dx != dy and (dx == 1 or dy == 1): + if dx != dy and dx <= 1 and dy <= 1: if game.country_turn_id == country.id and current_turn_movable: if grid.move_boat(data, mouse_pos): current_turn_movable = false @@ -37,6 +36,6 @@ func _draw(): Vector2.ZERO, 16, 0, 2*PI, 32, - Color.yellow, + Color.greenyellow, 4 ) diff --git a/prefabs/units/boat/boat.tscn b/prefabs/units/boat/boat.tscn index a203121..1f48f2f 100644 --- a/prefabs/units/boat/boat.tscn +++ b/prefabs/units/boat/boat.tscn @@ -16,5 +16,3 @@ shape = SubResource( 1 ) texture = ExtResource( 1 ) region_enabled = true region_rect = Rect2( 32, 32, 16, 16 ) - -[connection signal="input_event" from="." to="." method="_on_Boat_input_event"] diff --git a/project.godot b/project.godot index f4e4b6b..a1c46bc 100644 --- a/project.godot +++ b/project.godot @@ -9,6 +9,16 @@ config_version=4 _global_script_classes=[ { +"base": "Area2D", +"class": "Army", +"language": "GDScript", +"path": "res://prefabs/units/army/army.gd" +}, { +"base": "Area2D", +"class": "Boat", +"language": "GDScript", +"path": "res://prefabs/units/boat/boat.gd" +}, { "base": "Reference", "class": "Country", "language": "GDScript", @@ -20,6 +30,8 @@ _global_script_classes=[ { "path": "res://prefabs/grid/grid.gd" } ] _global_script_class_icons={ +"Army": "", +"Boat": "", "Country": "", "Grid": "" } diff --git a/scenes/game/game.gd b/scenes/game/game.gd index c98df25..8c2ef4d 100644 --- a/scenes/game/game.gd +++ b/scenes/game/game.gd @@ -47,6 +47,7 @@ func _on_EndTurnButton_pressed(): for boat in countries[country_turn_id].boats: boat.entity.current_turn_movable = true countries[country_turn_id].compute_balance() + $Grid.selected_thing = null country_turn_id += 1 if country_turn_id == 8: country_turn_id = 1 @@ -77,7 +78,7 @@ func _on_Grid_tile_pressed(v): var tile = $Grid.grid[v] match mouse_click_mode: "buy_factory": - if country.money >= 5 and tile.country == country and not tile.factory: + if country.money >= 5 and tile.country == country and not tile.needs_boat and not tile.factory: country.money -= 5 $Grid.set_factory(v, true) "buy_army": @@ -85,9 +86,20 @@ func _on_Grid_tile_pressed(v): country.money -= 3 country.add_unit({}, v) "buy_boat": - if country.money >= 3 and $Grid.has_adjacent_factory(v, country) and len(tile.boats) == 0: + if country.money >= 3 and $Grid.has_adjacent_factory(v, country) and tile.needs_boat and len(tile.boats) == 0: country.money -= 3 country.add_boat({}, v) + null: + if $Grid.selected_thing is Boat and $Grid.selected_thing.get_tile_position() == v or len(tile.boats) == 0: + for army in tile.units: + if army.country == country: + $Grid.selected_thing = army.entity + break + else: + for boat in tile.boats: + if boat.country == country: + $Grid.selected_thing = boat.entity + break func _input(event): if event is InputEventMouseButton: