diff --git a/prefabs/grid/grid.gd b/prefabs/grid/grid.gd index 3e36af6..f2dacf4 100644 --- a/prefabs/grid/grid.gd +++ b/prefabs/grid/grid.gd @@ -6,6 +6,7 @@ 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") onready var tilemap = $TileMap onready var total_scale = $TileMap.scale * $TileMap.cell_size var selected_thing @@ -126,14 +127,38 @@ func remove_unit(unit): unit.entity.queue_free() grid[v].units.erase(unit) -func add_boat(v, unit): +func add_boat(unit, v): grid[v].boats.append(unit) - update() + unit.entity = Boat.instance() + unit.entity.country = unit.country + unit.entity.position = v * total_scale + total_scale*0.5 + unit.grid = self + unit.entity.data = unit + unit.position = v + add_child(unit.entity) -func move_boat(v, v2, unit): +func boat_can_move_to(unit, destination): + var dest_tile = grid[destination] + return dest_tile.needs_boat and not tile_have_boat_of_country(dest_tile, unit.country) + +func move_boat(unit, destination): + var v = $TileMap.world_to_map(unit.entity.position/tilemap.scale) + var dest_tile = grid[destination] + if not boat_can_move_to(unit, destination): + return false grid[v].boats.erase(unit) - grid[v2].boats.append(unit) - update() + grid[destination].boats.append(unit) + unit.entity.position = destination * total_scale + total_scale*0.5 + unit.position = destination + return true + +func has_adjacent_factory(v, country): + for p in [Vector2(-1, 0), Vector2(1, 0),Vector2(0, -1), Vector2(0, 1)]: + var tile = grid[v+p] if v+p in grid else null + if tile: + if tile.country == country and tile.factory: + return true + return false func setup(): _setup_tileset_texture_2x() @@ -180,23 +205,8 @@ func _get_atlas_from_tileset(tileset, name, texture): func _draw(): var scale = $TileMap.scale * $TileMap.cell_size - var army_texture = _get_atlas_from_tileset($TileMap.tile_set, "army", tileset_texture_2x) - army_texture.region.position *= 2 - army_texture.region.size *= 2 - var boat_texture = _get_atlas_from_tileset($TileMap.tile_set, "boat", tileset_texture_2x) - boat_texture.region.position *= 2 - boat_texture.region.size *= 2 - for x in 8: for y in 8: var v = Vector2(x, y) var tile = grid[v] draw_circle(v * scale + scale*3/4, 8, tile.country.color) - - var unit_position = Vector2(0, 0) - for boat in tile.boats: - draw_texture(boat_texture, v*scale + unit_position + total_scale*0.25, boat.country.color) - unit_position += Vector2(8, 8) -# for unit in tile.units: -# draw_texture(army_texture, v*scale + unit_position, unit.country.color) -# unit_position += Vector2(8, 8) diff --git a/prefabs/units/boat/boat.gd b/prefabs/units/boat/boat.gd new file mode 100644 index 0000000..a575a8d --- /dev/null +++ b/prefabs/units/boat/boat.gd @@ -0,0 +1,42 @@ +extends Area2D + +var data#unit data +onready var grid = data.grid +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 _process(delta): + update() + if game.country_turn_id == country.id: + $Sprite.modulate = Color.white if current_turn_movable else Color.white.darkened(0.5) + else: + $Sprite.modulate = Color.white.darkened(0.25) + 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 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 game.country_turn_id == country.id and current_turn_movable: + if grid.move_boat(data, mouse_pos): + current_turn_movable = false + +func _draw(): + if grid.selected_thing == self: + draw_arc( + Vector2.ZERO, + 16, + 0, 2*PI, 32, + Color.yellow, + 4 + ) diff --git a/prefabs/units/boat/boat.tscn b/prefabs/units/boat/boat.tscn new file mode 100644 index 0000000..a203121 --- /dev/null +++ b/prefabs/units/boat/boat.tscn @@ -0,0 +1,20 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://assets/tileset.png" type="Texture" id=1] +[ext_resource path="res://prefabs/units/boat/boat.gd" type="Script" id=2] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 7, 5 ) + +[node name="Boat" type="Area2D"] +script = ExtResource( 2 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) + +[node name="Sprite" type="Sprite" parent="."] +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/scenes/game/country.gd b/scenes/game/country.gd index 89d71c4..e8db60b 100644 --- a/scenes/game/country.gd +++ b/scenes/game/country.gd @@ -20,6 +20,15 @@ func add_unit(unit, v): func remove_unit(unit): armies.erase(unit) grid.remove_unit(unit) + +func add_boat(unit, v): + unit.country = self + boats.append(unit) + grid.add_boat(unit, v) + +func remove_boat(unit): + boats.erase(unit) + grid.remove_boat(unit) func compute_balance(): money += len(factory_positions) - len(armies) - len(boats) diff --git a/scenes/game/game.gd b/scenes/game/game.gd index e6da42e..c98df25 100644 --- a/scenes/game/game.gd +++ b/scenes/game/game.gd @@ -44,6 +44,8 @@ func _process(delta): func _on_EndTurnButton_pressed(): for army in countries[country_turn_id].armies: army.entity.current_turn_movable = true + for boat in countries[country_turn_id].boats: + boat.entity.current_turn_movable = true countries[country_turn_id].compute_balance() country_turn_id += 1 @@ -82,7 +84,11 @@ func _on_Grid_tile_pressed(v): if country.money >= 3 and tile.country == country and tile.factory and len(tile.units) == 0: 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: + country.money -= 3 + country.add_boat({}, v) + func _input(event): if event is InputEventMouseButton: if event.pressed and event.button_index == BUTTON_RIGHT: