done from playerless game

master
potatoxel 7 months ago
parent 864613fcbf
commit e6daff3738
  1. 45
      prefabs/grid/grid.gd
  2. 29
      prefabs/units/army/army.gd
  3. 2
      prefabs/units/army/army.tscn
  4. 13
      prefabs/units/boat/boat.gd
  5. 2
      prefabs/units/boat/boat.tscn
  6. 12
      project.godot
  7. 16
      scenes/game/game.gd

@ -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:

@ -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:

@ -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"]

@ -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
)

@ -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"]

@ -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": ""
}

@ -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:

Loading…
Cancel
Save