This commit is contained in:
ProgramSnail 2023-08-10 12:18:17 +03:00
parent 5408d75267
commit a795665b54
7 changed files with 462 additions and 366 deletions

File diff suppressed because one or more lines are too long

View file

@ -32,6 +32,7 @@ mesh = ExtResource( 16 )
mesh = ExtResource( 11 )
[node name="castle" type="MeshInstance" parent="."]
transform = Transform( 0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, 0 )
mesh = ExtResource( 12 )
[node name="detail_forestA" type="MeshInstance" parent="."]

View file

@ -46,7 +46,7 @@ mesh_library = ExtResource( 7 )
cell_size = Vector3( 1, 1, 0.87 )
cell_center_y = false
data = {
"cells": PoolIntArray( 2, 1, 0, 6, 1, 0, 65532, 1, 1, 65534, 1, 1, 3, 3, 0, 5, 3, 0, 7, 3, 0, 65533, 3, 1, 2, 5, 1, 4, 5, 1, 6, 5, 0, 65534, 5, 1, 1, 7, 1, 3, 7, 1, 5, 7, 1, 2, 9, 1, 3, 65527, 1, 5, 65527, 1, 4, 65529, 1, 6, 65529, 0, 5, 65531, 0, 2, 65533, 0, 4, 65533, 0, 6, 65533, 0, 8, 65533, 2, 10, 65533, 2, 1, 65535, 0, 3, 65535, 0, 5, 65535, 0, 9, 65535, 2, 65533, 65535, 1, 65535, 65535, 1 )
"cells": PoolIntArray( 2, 1, 0, 6, 1, 0, 12, 1, 2, 65532, 1, 1, 65534, 1, 1, 3, 3, 0, 5, 3, 0, 7, 3, 0, 65533, 3, 1, 2, 5, 1, 4, 5, 1, 6, 5, 0, 65534, 5, 1, 1, 7, 1, 3, 7, 1, 5, 7, 1, 2, 9, 1, 3, 65527, 2, 5, 65527, 2, 4, 65529, 2, 6, 65529, 0, 5, 65531, 0, 2, 65533, 0, 4, 65533, 0, 6, 65533, 0, 8, 65533, 2, 10, 65533, 2, 12, 65533, 2, 1, 65535, 0, 3, 65535, 0, 5, 65535, 0, 9, 65535, 2, 11, 65535, 2, 13, 65535, 2, 65533, 65535, 1, 65535, 65535, 1 )
}
__meta__ = {
"_editor_clip_": 0,
@ -59,7 +59,7 @@ mesh_library = ExtResource( 1 )
cell_size = Vector3( 1, 1, 0.87 )
cell_center_y = false
data = {
"cells": PoolIntArray( 4, 5, 18, 3, 7, 11, 131062, 65528, 29, 9, 65535, 13 )
"cells": PoolIntArray( 4, 5, 18, 3, 7, 11, 131062, 65528, 29, 9, 65535, 2 )
}
__meta__ = {
"_editor_clip_": 0,
@ -75,7 +75,7 @@ mesh_library = ExtResource( 1 )
cell_size = Vector3( 1, 1, 0.87 )
cell_center_y = false
data = {
"cells": PoolIntArray( 0, 1, 10, 6, 1, 4, 65534, 1, 4, 1, 3, 10, 65535, 3, 10, 0, 5, 5, 5, 7, 4, 65535, 7, 10, 0, 9, 10, 5, 11, 17, 4, 65525, 10, 131062, 65528, 29, 6, 65529, 4, 3, 65531, 5, 11, 65531, 10, 2, 65533, 4, 8, 65533, 4 )
"cells": PoolIntArray( 0, 1, 10, 6, 1, 4, 65534, 1, 4, 1, 3, 10, 65535, 3, 10, 0, 5, 5, 12, 5, 16, 5, 7, 4, 11, 7, 4, 65535, 7, 10, 0, 9, 10, 10, 9, 10, 12, 9, 2, 5, 11, 17, 4, 65525, 10, 131062, 65528, 29, 6, 65529, 4, 3, 65531, 5, 11, 65531, 10, 2, 65533, 4, 8, 65533, 4 )
}
__meta__ = {
"_editor_clip_": 0,
@ -87,7 +87,7 @@ mesh_library = ExtResource( 2 )
cell_size = Vector3( 1, 1, 0.87 )
cell_center_y = false
data = {
"cells": PoolIntArray( 0, 1, 0, 4, 1, 3, 8, 1, 3, 10, 1, 3, 12, 1, 3, 65530, 1, 3, 1, 3, 0, 9, 3, 3, 11, 3, 3, 65531, 3, 3, 65535, 3, 0, 0, 5, 0, 8, 5, 3, 65532, 5, 3, 7, 7, 3, 65531, 7, 3, 65533, 7, 3, 65535, 7, 0, 0, 9, 0, 4, 9, 3, 6, 9, 3, 8, 9, 3, 65532, 9, 3, 65534, 9, 3, 1, 11, 3, 3, 11, 3, 5, 11, 0, 7, 11, 3, 65535, 11, 3, 4, 13, 3, 6, 13, 3, 1, 65523, 3, 3, 65523, 3, 5, 65523, 3, 2, 65525, 3, 4, 65525, 0, 6, 65525, 3, 8, 65525, 3, 1, 65527, 3, 7, 65527, 3, 11, 65527, 3, 13, 65527, 3, 2, 65529, 3, 8, 65529, 3, 10, 65529, 3, 12, 65529, 3, 1, 65531, 3, 3, 65531, 0, 7, 65531, 3, 9, 65531, 3, 11, 65531, 0, 13, 65531, 3, 65535, 65531, 3, 0, 65533, 3, 12, 65533, 3, 65532, 65533, 3, 65534, 65533, 3, 7, 65535, 3, 11, 65535, 3, 65531, 65535, 3 )
"cells": PoolIntArray( 0, 1, 0, 4, 1, 3, 8, 1, 3, 10, 1, 3, 14, 1, 3, 16, 1, 3, 65530, 1, 3, 1, 3, 0, 9, 3, 3, 11, 3, 3, 13, 3, 3, 15, 3, 3, 17, 3, 3, 65531, 3, 3, 65535, 3, 0, 0, 5, 0, 8, 5, 3, 10, 5, 3, 12, 5, 0, 14, 5, 3, 16, 5, 3, 18, 5, 3, 65532, 5, 3, 7, 7, 3, 9, 7, 3, 11, 7, 0, 13, 7, 3, 15, 7, 3, 17, 7, 3, 65531, 7, 3, 65533, 7, 3, 65535, 7, 0, 0, 9, 0, 4, 9, 3, 6, 9, 3, 8, 9, 3, 10, 9, 0, 12, 9, 0, 14, 9, 3, 65532, 9, 3, 65534, 9, 3, 1, 11, 3, 3, 11, 3, 5, 11, 0, 7, 11, 3, 9, 11, 3, 11, 11, 3, 13, 11, 3, 15, 11, 3, 65535, 11, 3, 4, 13, 3, 6, 13, 3, 10, 13, 3, 12, 13, 3, 11, 15, 3, 1, 65523, 3, 3, 65523, 3, 5, 65523, 3, 2, 65525, 3, 4, 65525, 0, 6, 65525, 3, 8, 65525, 3, 1, 65527, 3, 7, 65527, 3, 11, 65527, 3, 13, 65527, 3, 2, 65529, 3, 8, 65529, 3, 10, 65529, 3, 12, 65529, 3, 1, 65531, 3, 3, 65531, 0, 7, 65531, 3, 9, 65531, 3, 11, 65531, 0, 13, 65531, 3, 17, 65531, 3, 65535, 65531, 3, 0, 65533, 3, 14, 65533, 3, 16, 65533, 3, 18, 65533, 3, 65532, 65533, 3, 65534, 65533, 3, 7, 65535, 3, 15, 65535, 3, 17, 65535, 3, 65531, 65535, 3 )
}
__meta__ = {
"_editor_clip_": 0,

View file

@ -28,6 +28,39 @@ func is_current_character_exist():
func spawn_current_player_character(position : Vector3):
player_characters[current_player].append( \
utils.spawn_on_position(character_scene, position))
func try_merge_current_character_with(character : int):
if current_character == character:
return false
if current_player_used_characters.has(current_character) and \
current_player_used_characters.has(character):
return false
if player_characters[current_player][current_character].level != player_characters[current_player][character].level:
return false
if not player_characters[current_player][current_character].upgrade():
return false
player_characters[current_player][current_character].translation = player_characters[current_player][character].translation
var is_active_after_merge = not current_player_used_characters.has(current_character) and \
not current_player_used_characters.has(character)
player_characters[current_player][character].queue_free()
if character + 1 != current_player_characters_count():
player_characters[current_player][character] = player_characters[current_player].back()
if current_character + 1 == current_player_characters_count():
current_character = character
player_characters[current_player].pop_back()
if not is_active_after_merge:
set_next_character()
return true
func destroy_characters_on_position(characters : Array, position : Vector3):
var destroyed_count = 0
@ -131,8 +164,6 @@ func set_next_player():
current_character_obj().active = false
map.player_tiles[current_player] = map.DEFAULT_PLAYER_TILES[current_player]
print(map.player_tiles[current_player])
print(map.DEFAULT_PLAYER_TILES[current_player])
map.remove_active_player_color(current_player)
current_player_used_characters.clear()

View file

@ -2,8 +2,6 @@ extends Spatial
# TODO: create something like Vector3i
var building_destruction_scene = preload("res://scenes/destruction.tscn")
onready var characters = get_node("/root/Level/Map/Characters")
onready var money = get_node("/root/Level/Map/Money")
onready var utils = get_node("/root/Level/Map/Utils")
@ -77,9 +75,13 @@ func start_player_turn(player : int):
money.pay_tower_salaries(player)
money.pay_character_salaries(player)
func are_tiles_connected(start_position : Vector3, end_position : Vector3, between_tile_cell : int):
if start_position.is_equal_approx(end_position):
return true
func is_tile_connected_to_tiles(start_position : Vector3, end_positions : Array, between_tile_cell : int):
var end_positions_dict = {}
for end_position in end_positions:
end_positions_dict[utils.position_to_string(end_position)] = null
if end_positions_dict.has(utils.position_to_string(start_position)):
return true
var visited = {}
@ -88,7 +90,7 @@ func are_tiles_connected(start_position : Vector3, end_position : Vector3, betwe
for current_position in to_visit:
for direction in directions:
if (current_position + direction).is_equal_approx(end_position):
if end_positions_dict.has(utils.position_to_string(current_position + direction)):
return true
if get_tile_cell(current_position + direction) == between_tile_cell and \
not visited.has(utils.position_to_string(current_position + direction)):
@ -96,6 +98,9 @@ func are_tiles_connected(start_position : Vector3, end_position : Vector3, betwe
to_visit.append(current_position + direction)
return false
func are_tiles_connected(start_position : Vector3, end_position : Vector3, between_tile_cell : int):
return is_tile_connected_to_tiles(start_position, [end_position], between_tile_cell)
func is_connected_to_house(start_position : Vector3, tile_cell : int):
var visited = {}
@ -131,62 +136,12 @@ func tile_block_level(position : Vector3, inviding_player : int):
return block_level
func move_to_tile(position : Vector3):
var tile_cell = get_tile_cell(position)
var building_cell = get_building_cell(position)
var world_position = utils.grid_to_world_position(position)
# tile to move should be connected with current character tile
if not are_tiles_connected(utils.world_to_grid_position(characters.current_character_obj().translation), position, current_player_tile()):
return
# can't go on tile, blocked with >= level, except characters with MAX_LEVEL
if tile_block_level(position, characters.current_player) >= characters.current_character_obj().level and \
characters.current_character_obj().level != characters.current_character_obj().MAX_LEVEL:
return
if tile_cell == GridMap.INVALID_CELL_ITEM:
return
# move without action (to current player tile)
if tile_cell == current_player_tile() and \
building_cell == GridMap.INVALID_CELL_ITEM:
if characters.destroy_other_player_characters(position, characters.current_player) > 0:
utils.spawn_on_position(building_destruction_scene, position)
characters.current_character_obj().translation = world_position
# move with action (to tile of other player)
elif tile_cell != GridMap.INVALID_CELL_ITEM and \
tile_cell != current_player_tile() and \
has_neighbour_tile_cell(position, current_player_tile()):
set_tile_cell(position, current_player_tile())
if building_cell != GridMap.INVALID_CELL_ITEM or \
characters.destroy_other_player_characters(position, characters.current_player) > 0:
utils.spawn_on_position(building_destruction_scene, position)
if building_cell != GridMap.INVALID_CELL_ITEM:
money.destroy_house(position)
set_building_cell(position, GridMap.INVALID_CELL_ITEM)
characters.current_character_obj().translation = world_position
characters.turn_characters_into_rogues()
characters.set_next_character()
func current_player_tile():
return player_tiles[characters.current_player]
func remove_active_player_color(player : int):
print("Player tile: ", player_tiles[player])
for tile_position in tiles.get_used_cells():
if get_tile_cell(tile_position) == ACTIVE_PLAYER_TILES[player]:
print("Active tile removed")
set_tile_cell(tile_position, DEFAULT_PLAYER_TILES[player])
func set_active_player_color(player : int):

View file

@ -28,7 +28,9 @@ func pay_tower_salaries(player : int):
func pay_character_salaries(player : int):
var alive_characters = []
var new_rogues_positions = []
# find new rogue characters (not fully paid characters)
for character in characters.player_characters[player]:
var character_position = utils.world_to_grid_position(character.translation)
if spend_connected_money(character_position, map.player_tiles[player], CHARACTER_SALARIES[character.level]):
@ -36,6 +38,22 @@ func pay_character_salaries(player : int):
else:
character.became_rogue()
characters.rogue_characters.append(character)
new_rogues_positions.append(character_position)
characters.player_characters[player] = alive_characters
# better solution ??
# find characters, connected to new rogues (not fully paid characters)
alive_characters = []
for character in characters.player_characters[player]:
var character_position = utils.world_to_grid_position(character.translation)
if map.is_tile_connected_to_tiles(character_position,
new_rogues_positions,
map.player_tiles[player]):
character.became_rogue()
characters.rogue_characters.append(character)
else:
alive_characters.append(character)
characters.player_characters[player] = alive_characters
@ -85,6 +103,28 @@ func spend_connected_money(starting_position : Vector3, tile_cell : int, money_t
to_visit.append(current_position + direction)
return money_to_spend <= 0
func add_money_to_nearest_other_house(starting_position : Vector3, tile_cell : int, money_added : int):
if money_added <= 0:
return
var visited = {}
var to_visit = [starting_position]
visited[utils.position_to_string(starting_position)] = null
for current_position in to_visit:
for direction in map.directions:
var new_position = current_position + direction
if map.get_tile_cell(new_position) == tile_cell and \
not visited.has(utils.position_to_string(new_position)):
visited[utils.position_to_string(new_position)] = null
to_visit.append(new_position)
if map.HOUSE_CELLS.has(map.get_building_cell(new_position)):
houses_money[utils.position_to_string(new_position)] += money_added
update_house_info(new_position)
return
func update_house_info(position : Vector3):
var position_string = utils.position_to_string(position)

View file

@ -1,10 +1,66 @@
extends Spatial
var building_destruction_scene = preload("res://scenes/destruction.tscn")
onready var map = get_node("/root/Level/Map")
onready var characters = get_node("/root/Level/Map/Characters")
onready var money = get_node("/root/Level/Map/Money")
onready var utils = get_node("/root/Level/Map/Utils")
func move_to_tile(position : Vector3):
var tile_cell = map.get_tile_cell(position)
var building_cell = map.get_building_cell(position)
var world_position = utils.grid_to_world_position(position)
# tile to move should be connected with current character tile
if not map.are_tiles_connected(utils.world_to_grid_position(characters.current_character_obj().translation),
position,
map.current_player_tile()):
return
# can't go on tile, blocked with >= level, except characters with MAX_LEVEL
if map.tile_block_level(position, characters.current_player) >= characters.current_character_obj().level and \
characters.current_character_obj().level != characters.current_character_obj().MAX_LEVEL:
return
if tile_cell == GridMap.INVALID_CELL_ITEM:
return
# move without action (to current player tile)
if tile_cell == map.current_player_tile() and \
building_cell == GridMap.INVALID_CELL_ITEM:
characters.current_character_obj().translation = world_position
if characters.destroy_other_player_characters(position, characters.current_player) > 0:
characters.set_next_character()
utils.spawn_on_position(building_destruction_scene, position)
# move with action (to tile of other player)
elif tile_cell != GridMap.INVALID_CELL_ITEM and \
tile_cell != map.current_player_tile() and \
map.has_neighbour_tile_cell(position, map.current_player_tile()):
map.set_tile_cell(position, map.current_player_tile())
characters.current_character_obj().translation = world_position
if building_cell != GridMap.INVALID_CELL_ITEM or \
characters.destroy_other_player_characters(position, characters.current_player) > 0:
utils.spawn_on_position(building_destruction_scene, position)
if building_cell != GridMap.INVALID_CELL_ITEM:
money.add_money_to_nearest_other_house(position,
map.current_player_tile(),
money.houses_money[utils.position_to_string(position)])
money.destroy_house(position)
map.set_building_cell(position, GridMap.INVALID_CELL_ITEM)
characters.turn_characters_into_rogues()
characters.set_next_character()
func make_turn(position : Vector3):
if map.is_connected_to_house(position, map.current_player_tile()):
var mouse_position_character_and_player_ids = characters.find_character_and_player_ids_on(position)
@ -15,7 +71,7 @@ func make_turn(position : Vector3):
characters.switch_character_to(mouse_position_character_and_player_ids[0],
mouse_position_character_and_player_ids[1])
elif characters.is_current_character_exist():
map.move_to_tile(position)
move_to_tile(position)
func upgrade_character_on_position(position : Vector3):
var character_on_position = characters.find_character_on(position)
@ -26,19 +82,32 @@ func upgrade_character_on_position(position : Vector3):
character_on_position.upgrade():
money.spend_connected_money(position, map.current_player_tile(), money.CHARACTER_COST)
func try_spawn_character(position : Vector3):
if map.get_tile_cell(position) == map.current_player_tile() and \
map.get_building_cell(position) == GridMap.INVALID_CELL_ITEM and \
map.is_connected_to_house(position, map.current_player_tile()) and \
characters.find_character_and_player_ids_on(position) == null and \
money.count_connected_money(position, map.current_player_tile()) >= money.CHARACTER_COST:
func spawn_character(position : Vector3):
money.spend_connected_money(position, map.current_player_tile(), money.CHARACTER_COST)
characters.spawn_current_player_character(position)
characters.switch_character_to(characters.current_player, \
characters.current_player_characters_count() - 1)
money.spend_connected_money(position, map.current_player_tile(), money.CHARACTER_COST)
characters.spawn_current_player_character(position)
if characters.current_character + 1 == characters.current_player_characters_count():
characters.current_character_obj().active = true
if characters.destroy_other_player_characters(position, characters.current_player) > 0:
characters.set_next_character()
utils.spawn_on_position(building_destruction_scene, position)
func handle_spawn(position : Vector3):
if map.get_tile_cell(position) == map.current_player_tile():
var character_and_player_ids_on_position = characters.find_character_and_player_ids_on(position)
if character_and_player_ids_on_position == null: # try spawn character
if map.get_building_cell(position) == GridMap.INVALID_CELL_ITEM and \
map.is_connected_to_house(position, map.current_player_tile()) and \
money.count_connected_money(position, map.current_player_tile()) >= money.CHARACTER_COST:
spawn_character(position)
else: # try merge characters
if characters.is_current_character_exist() and \
character_and_player_ids_on_position[0] == characters.current_player:
characters.try_merge_current_character_with(character_and_player_ids_on_position[1])
else: # TODO: spawn and move character
pass
func _physics_process(_delta):
if Input.is_action_just_released("turn"):
@ -51,4 +120,4 @@ func _physics_process(_delta):
characters.set_next_player()
if Input.is_action_just_released("spawn"):
try_spawn_character(utils.world_to_grid_position(utils.mouse_position()))
handle_spawn(utils.world_to_grid_position(utils.mouse_position()))