268 lines
6.9 KiB
GDScript
268 lines
6.9 KiB
GDScript
extends Node
|
|
|
|
signal updateMap
|
|
signal updateUI
|
|
|
|
var Server = false #true if this is the server isntances
|
|
const UIBorder = 1310
|
|
const travelTime = 10
|
|
|
|
|
|
var worldmap
|
|
var worldmapLoaded = false
|
|
|
|
|
|
var worldTiles = {} #currently managed with strings, can be indexed by ints in Godotscript
|
|
var selectedRegion = null
|
|
var armiesOnMap = {}
|
|
var armies = {}
|
|
var selectedArmy = null
|
|
var selecterArmydiplo = ''
|
|
|
|
|
|
func addArmy(from, to, army, armydata, player=true):
|
|
var arrive = from['turn'] + travelTime
|
|
var regionturn = to['turn']
|
|
if arrive <= regionturn:
|
|
var armies = to['armies']
|
|
armies[army] = armydata
|
|
setPresent(to)
|
|
if player:
|
|
if (to['playerTurnOn'] == -1) or (to['playerTurnOn'] > regionturn):
|
|
to['playerTurnOn'] = regionturn
|
|
else:
|
|
if (to['playerTurnOn'] == -1) or (to['playerTurnOn'] > arrive):
|
|
to['turn'] = arrive
|
|
var armies = to['armies']
|
|
#print(armies)
|
|
# if not typeof(armies) == TYPE_DICTIONARY:
|
|
# to['armies']={}
|
|
# armies = to['armies']
|
|
armies[army] = armydata
|
|
setPresent(to)
|
|
if player:
|
|
to['playerTurnOn'] = regionturn
|
|
else:
|
|
if not to.has('incomming'):
|
|
to['incomming'] = {}
|
|
var incomming = to['incomming']
|
|
armydata['arrive'] = arrive
|
|
incomming[army] = armydata
|
|
if player:
|
|
addFaction(to, armydata['faction'])
|
|
emit_signal("updateMap")
|
|
emit_signal("updateUI")
|
|
|
|
|
|
func addTile(response):
|
|
var x = ''
|
|
if not worldTiles.has(String(response['x'])):
|
|
worldTiles[String(response['x'])] = {}
|
|
x = worldTiles[String(response['x'])]
|
|
x[String(response['y'])] = response
|
|
|
|
|
|
func addFaction(tile, factionName):
|
|
if not tile.factions.has(factionName):
|
|
tile.factions.append(factionName)
|
|
|
|
#func loadAsServer():
|
|
# var path = 'user://worldmap.json'
|
|
# var directory = Directory.new()
|
|
# var hasFile = directory.file_exists(path)
|
|
# var file = File.new() #this should be handled in in firstStart
|
|
# if !hasFile:
|
|
# path = directory.get_current_dir()+'preset//worldmap.json'
|
|
# file.open(path, file.READ)
|
|
# var json = file.get_as_text()
|
|
## print('json:'+json)
|
|
# var json_result = JSON.parse(json).result
|
|
# #print(json_result)
|
|
# for tile in json_result:
|
|
# #ensure everyone who has an army is counted as present, can be removed once the player update system is properly implemented
|
|
# var present = []
|
|
# if tile.has('armies'):
|
|
# for army in tile['armies']:
|
|
# if not present.has(army['faction']):
|
|
# present.append(army['faction'])
|
|
# tile['present'] = present
|
|
# WorldManager.addTile(tile)
|
|
# #to do: function to load regions
|
|
|
|
|
|
|
|
func getTileOrNull(x,y):
|
|
var tile = null
|
|
x = String(x)
|
|
y = String(y)
|
|
print("find ",x," ", y)
|
|
if worldTiles.has(x):
|
|
var row = worldTiles[x]
|
|
print(row)
|
|
if row.has(y):
|
|
tile = row[y]
|
|
print("tileOrNull not null")
|
|
return tile
|
|
|
|
func getTileGuaranteed(x, y):
|
|
var tile = null
|
|
tile = getTileOrNull(x,y)
|
|
if tile == null:
|
|
tile = {
|
|
"x":x,
|
|
"y":y,
|
|
"terrain":"FOREST",
|
|
"factions":[],
|
|
"turn":1,
|
|
"armies":{},
|
|
"present":{},
|
|
"playerTurnOn":-1
|
|
}
|
|
addTile(tile)
|
|
return tile
|
|
|
|
remote func moveArmy(fromX, fromY, toX, toY, army):
|
|
var from = getTileOrNull(fromX, fromY)
|
|
print("request army move")
|
|
print("tile: ", from)
|
|
if not from == null:
|
|
if from.has('armies'):
|
|
var armies = from['armies']
|
|
if armies.has(army):
|
|
var armydata = armies[army]
|
|
if PlayerManager.confirmFaction(get_tree().get_rpc_sender_id(), armydata['faction']):
|
|
print("confirm move")
|
|
var targetTile = getTileGuaranteed(toX, toY)
|
|
print("target is: ",targetTile)
|
|
from['armies'].erase(army)
|
|
removeArmy(from, army)
|
|
addArmy(from, targetTile, army, armydata)
|
|
updateLocalPlayers(from)
|
|
updateLocalPlayers(targetTile)
|
|
pass
|
|
|
|
remote func receiveMap(tiles):
|
|
for tile in tiles:
|
|
addTile(tile)
|
|
emit_signal("updateMap")
|
|
emit_signal("updateUI")
|
|
|
|
|
|
func updateLocalPlayers(tile):
|
|
var factions = tile.present
|
|
var connections = PlayerManager.clients_factions
|
|
for faction in factions:
|
|
for connection in connections:
|
|
if connections[connection]==faction:
|
|
if not ((connection == 1) or (connection==0)):
|
|
rpc_id(connection, 'receiveMap', tile)
|
|
pass
|
|
|
|
func removeArmy(tile, army):
|
|
var armies = tile.armies
|
|
armies.erase(army)
|
|
setPresent(tile)
|
|
# addTile(tile)
|
|
# print(getTileGuaranteed(0,0))
|
|
|
|
|
|
func requestMapfromServer():
|
|
rpc_id(1, 'requestMap', PlayerManager.authcache['response']['faction'])
|
|
|
|
func requestArmyMove(moveX=0, moveY=0):
|
|
print("move army ", selectedArmy)
|
|
#print("from ")
|
|
var error = false
|
|
if not selectedRegion == null:
|
|
print("x: ", selectedRegion.x, "Y: ", selectedRegion.y)
|
|
print("to: ", selectedRegion.x + moveX," / ", selectedRegion.y + moveY)
|
|
if not selectedArmy == null:
|
|
if not Server:
|
|
rpc_id(1, 'moveArmy', selectedRegion.x, selectedRegion.y, selectedRegion.x+moveX, selectedRegion.y + moveY, selectedArmy)
|
|
else:
|
|
moveArmy(selectedRegion.x, selectedRegion.y, selectedRegion.x+moveX, selectedRegion.y + moveY, selectedArmy)
|
|
else:
|
|
print("regionselecterror")
|
|
|
|
remote func requestMap(factionToFind):
|
|
if PlayerManager.clients_factions.has(get_tree().get_rpc_sender_id()):
|
|
if PlayerManager.clients_factions[get_tree().get_rpc_sender_id()] == factionToFind:
|
|
print('right faction')
|
|
else:
|
|
print('found instead:', PlayerManager.clients_factions[get_tree().get_rpc_sender_id()])
|
|
else:
|
|
print('only contains: ', PlayerManager.clients_factions)
|
|
var tiles = []
|
|
for x in worldTiles:
|
|
var row = worldTiles[x]
|
|
for y in row:
|
|
var tile = row[y]
|
|
#print('looking for map data: ', factionToFind)
|
|
#print(tile)
|
|
var factions = tile.factions
|
|
for faction in factions:
|
|
#print('comparing: ', faction, factionToFind)
|
|
if faction == factionToFind:
|
|
tiles.append(tile)
|
|
#print('appending: ',tile)
|
|
rpc_id(get_tree().get_rpc_sender_id(), 'receiveMap', tiles)
|
|
|
|
|
|
func setPresent(tile):
|
|
var armies = tile.armies
|
|
var present = {}
|
|
for army in armies:
|
|
var armyValue = 1#TO DO: have method to calculate army strength
|
|
var armydata = armies[army]
|
|
var faction = armydata.faction
|
|
if present.has(faction):
|
|
present[faction] += armyValue
|
|
else:
|
|
present[faction] = armyValue
|
|
tile['present']=present
|
|
return
|
|
|
|
|
|
func _load_worldmap():
|
|
get_tree().change_scene('res://Worldmap.tscn')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func requestTiles(unit = null):
|
|
rpc_id(1, 'requestTiles', unit)
|
|
|
|
signal receiveTiles
|
|
|
|
func receiveTiles(bundledTileStuff):
|
|
var newTileRect = bundledTileStuff[0]
|
|
var newTiles = bundledTileStuff[1]
|
|
var newSuperTiles = bundledTileStuff[2]
|
|
emit_signal("receiveTiles", newTileRect, newTiles, newSuperTiles)
|
|
|
|
func requestUnits():
|
|
rpc_id(1, 'requestUnits')
|
|
|
|
signal receiveUnits
|
|
|
|
func receiveUnits(newBoardUnits):
|
|
var boardUnits = newBoardUnits
|
|
for child in $TileMap/Units.get_children():
|
|
child.queue_free()
|
|
var boardUnitsIndex = 0
|
|
for unit in boardUnits:
|
|
var newUnit
|
|
if unit.superUnit:
|
|
newUnit = (load("res://SuperUnit.tscn")).instance()
|
|
else:
|
|
newUnit = (load("res://Unit.tscn")).instance()
|
|
newUnit.boardUnitsIndex = boardUnitsIndex
|
|
$TileMap/Units.add_child(newUnit)
|
|
newUnit.on_ready()
|
|
boardUnitsIndex +=1
|
|
emit_signal("receiveUnits", newBoardUnits)
|