|
|
|
@ -3,7 +3,6 @@ extends Node2D |
|
|
|
|
|
|
|
|
|
signal tile_pressed(v) |
|
|
|
|
|
|
|
|
|
var tileset_texture_2x: ImageTexture |
|
|
|
|
var grid: Dictionary#<Vector2, TileInfo> |
|
|
|
|
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: |
|
|
|
|