1
0
Fork 0

Added combat simulation display

This commit is contained in:
Michaël Lemaire 2020-02-18 23:24:00 +01:00
parent db3ec0aa67
commit c31a4740ae
10 changed files with 181 additions and 96 deletions

2
ai/dumb.gd Normal file
View file

@ -0,0 +1,2 @@
static func play(arena, hero, hand):
hand.emit_turn_end()

View file

@ -1,7 +1,7 @@
tool
extends Node2D
signal played(card, anchor)
signal played(card, anchor, unit)
const AnimHelper = preload("res://helpers/anims.gd")
const BattleHelper = preload("res://helpers/battle.gd")
@ -65,11 +65,14 @@ func update_anchors():
selected_anchor = BattleHelper.update_anchors(get_tree(), position, funcref(self, "can_be_used_on_anchor"))
func play(anchor):
var unit
if spawned_unit:
var unit = spawned_unit.instance()
unit = spawned_unit.instance()
unit.attacker = hero.attacker if hero else false
anchor.set_content(unit)
emit_signal("played", self, anchor)
emit_signal("played", self, anchor, unit)
func return_to_base():
AnimHelper.linear_goto(self, base_position, 0.3)

View file

@ -40,6 +40,25 @@ static func list_units(tree: SceneTree):
return units
static func apply_combat(attacker: UnitPoints, defender: UnitPoints):
""" Apply a unit combat to points
"""
while attacker.damage:
attacker.damage -= 1
if defender.shield:
defender.shield -= 1
elif defender.hull:
defender.hull -= 1
while defender.damage:
defender.damage -= 1
if attacker.shield:
attacker.shield -= 1
elif attacker.hull:
attacker.hull -= 1
static func get_state(tree: SceneTree):
""" Build the simple state associated with current battle scene
"""

View file

@ -4,6 +4,7 @@ const BattleHelper = preload("res://helpers/battle.gd")
export(PackedScene) var deck_attack
export(PackedScene) var deck_defend
export(Script) var defend_ai
onready var deck_attack_cards = deck_attack.instance()
onready var deck_defend_cards = deck_defend.instance()
@ -15,12 +16,12 @@ func _ready():
hero_attack = create_hero_unit("tomahawk", "attack_start", true)
hero_defend = create_hero_unit("rhino", "defend_start", false)
fill_hand($hand_attack, deck_attack_cards, hero_attack)
fill_hand($hand_defend, deck_defend_cards, hero_defend)
$hand_attack.connect("turn_end", self, "end_turn")
$hand_defend.connect("turn_end", self, "end_turn")
$combat_info.visible = false
fill_hands()
adjust_playable()
#print(BattleHelper.get_state(get_tree()))
@ -34,7 +35,9 @@ func find_free_anchor(anchor_type: String):
func create_hero_unit(name: String, anchor_type: String, attacker: bool):
var anchor = find_free_anchor(anchor_type)
if anchor:
return BattleHelper.spawn_unit("heroes/" + name, anchor, attacker)
var unit = BattleHelper.spawn_unit("heroes/" + name, anchor, attacker)
unit.connect("combat_simulation", self, "_on_combat_simulation")
return unit
else:
return null
@ -48,12 +51,36 @@ func fill_hand(hand: Node, deck: Node, hero: Node, limit=4):
hand.rearrange()
func fill_hands():
fill_hand($hand_attack, deck_attack_cards, hero_attack)
fill_hand($hand_defend, deck_defend_cards, hero_defend)
func end_turn():
turn_attack = not turn_attack
fill_hands()
adjust_playable()
for unit in BattleHelper.list_units(get_tree()):
unit.turn_ended()
func _process(delta):
if not turn_attack and defend_ai:
defend_ai.play($arena, hero_defend, $hand_defend)
func adjust_playable():
$hand_attack.visible = turn_attack
for unit in BattleHelper.list_units(get_tree()):
unit.playable = unit.attacker == turn_attack
func _on_unit_created(unit):
unit.connect("combat_simulation", self, "_on_combat_simulation")
func _on_combat_simulation(attacker, defender):
if attacker and defender:
$combat_info/attacker_points.set_from(attacker.get_points())
$combat_info/defender_points.set_from(defender.get_points())
BattleHelper.apply_combat($combat_info/attacker_points, $combat_info/defender_points)
$combat_info.visible = true
$hand_attack.visible = false
else:
$combat_info.visible = false
$hand_attack.visible = turn_attack

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=8 format=2]
[gd_scene load_steps=10 format=2]
[ext_resource path="res://screens/battle/mechanolith.ogg" type="AudioStream" id=1]
[ext_resource path="res://theme/ui.tres" type="Theme" id=2]
@ -6,6 +6,8 @@
[ext_resource path="res://screens/battle/background.jpg" type="Texture" id=4]
[ext_resource path="res://screens/battle/battle.gd" type="Script" id=5]
[ext_resource path="res://screens/battle/hand.gd" type="Script" id=6]
[ext_resource path="res://units/points.tscn" type="PackedScene" id=7]
[ext_resource path="res://ai/dumb.gd" type="Script" id=8]
[ext_resource path="res://decks/all.tscn" type="PackedScene" id=9]
[node name="view" type="Panel"]
@ -14,10 +16,12 @@ anchor_bottom = 1.0
theme = ExtResource( 2 )
script = ExtResource( 5 )
__meta__ = {
"_edit_lock_": true,
"_edit_use_anchors_": false
}
deck_attack = ExtResource( 9 )
deck_defend = ExtResource( 9 )
defend_ai = ExtResource( 8 )
[node name="background" type="Sprite" parent="."]
position = Vector2( 540, 970 )
@ -27,7 +31,7 @@ __meta__ = {
"_edit_lock_": true
}
[node name="arena_normal1" parent="." instance=ExtResource( 3 )]
[node name="arena" parent="." instance=ExtResource( 3 )]
[node name="bgm" type="AudioStreamPlayer" parent="."]
stream = ExtResource( 1 )
@ -69,3 +73,20 @@ script = ExtResource( 6 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="combat_info" type="Control" parent="."]
anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="attacker_points" parent="combat_info" instance=ExtResource( 7 )]
position = Vector2( 260, -167.5 )
[node name="defender_points" parent="combat_info" instance=ExtResource( 7 )]
position = Vector2( 840, -167.5 )
[connection signal="unit_created" from="hand_attack" to="." method="_on_unit_created"]
[connection signal="pressed" from="hand_attack/skip" to="hand_attack" method="emit_turn_end"]
[connection signal="unit_created" from="hand_defend" to="." method="_on_unit_created"]

View file

@ -2,15 +2,12 @@ extends Control
const NodesHelpers = preload("res://helpers/nodes.gd")
signal unit_created(unit)
signal turn_end
export var attack = false
var cards = []
func _ready():
if has_node("skip"):
$skip.connect("pressed", self, "emit_turn_end")
func add_card(card):
""" Add a card to the hand
"""
@ -31,12 +28,14 @@ func rearrange():
card.set_hand_location(Vector2(i * 210, 0))
i += 1
func on_card_played(card, anchor):
func on_card_played(card, anchor, unit):
""" Called when a card is played
"""
card.queue_free()
cards.erase(card)
rearrange()
if unit:
emit_signal("unit_created", unit)
emit_turn_end()
func emit_turn_end():

View file

@ -3,6 +3,8 @@ extends Node2D
class_name BaseUnit
signal combat_simulation(attacker, defender)
const AnimHelper = preload("res://helpers/anims.gd")
const BattleHelper = preload("res://helpers/battle.gd")
@ -38,10 +40,7 @@ func destroy():
func set_attacker(val):
attacker = val
if has_node("points"):
if attacker:
$points.modulate = Color(1.0, 1.0, 1.0)
else:
$points.modulate = Color(1.0, 0.3, 0.3)
$points.enemy = not attacker
func set_playable(val):
playable = val
@ -88,14 +87,25 @@ func update_anchors():
selected_anchor = BattleHelper.update_anchors(get_tree(), dragged, funcref(self, "can_be_used_on_anchor"))
func play(anchor):
if playable:
if playable and anchor:
if anchor.is_empty():
move_to(anchor)
else:
var other = anchor.get_content()
if other.has_method("get_points"):
attack(other)
return_to_base()
reset_state()
func simulate(anchor):
if playable and anchor and not anchor.is_empty():
var other = anchor.get_content()
if other.has_method("get_points"):
emit_signal("combat_simulation", self, other)
else:
emit_signal("combat_simulation", null, null)
else:
emit_signal("combat_simulation", null, null)
func move_to(anchor):
if $points.move:
@ -112,47 +122,36 @@ func get_points() -> UnitPoints:
else:
return null
func attack(other) -> bool:
func attack(other):
var selfpoints = get_points()
var otherpoints = other.get_points()
while selfpoints.damage:
selfpoints.damage -= 1
if otherpoints.shield:
otherpoints.shield -= 1
elif otherpoints.hull:
otherpoints.hull -= 1
while otherpoints.damage:
otherpoints.damage -= 1
if selfpoints.shield:
selfpoints.shield -= 1
elif selfpoints.hull:
selfpoints.hull -= 1
BattleHelper.apply_combat(selfpoints, otherpoints)
if selfpoints.hull <= 0:
destroy()
if otherpoints.hull <= 0:
other.destroy()
return selfpoints.hull > 0 && otherpoints.hull == 0
func return_to_base():
func reset_state():
dragged = null
$move_hint.visible = false
$move_hint.set_point_position(1, Vector2(0, 0))
func turn_ended():
$points.move = base_move_points
$points.damage = base_damage_points
$points.shield = base_shield_points
func on_dragged(active, relative, absolute):
if not playable:
return
if dragged and not active:
if selected_anchor:
play(selected_anchor)
else:
return_to_base()
if active:
simulate(selected_anchor)
elif dragged:
simulate(null)
play(selected_anchor)
$move_hint.visible = active

View file

@ -7,12 +7,14 @@ export var move = 0 setget set_move
export var hull = 0 setget set_hull
export var shield = 0 setget set_shield
export var damage = 0 setget set_damage
export var enemy = false setget set_enemy
func _ready():
set_move(move)
set_hull(hull)
set_shield(shield)
set_damage(damage)
set_enemy(enemy)
func set_move(val):
move = val
@ -34,3 +36,16 @@ func set_damage(val):
if has_node("hud/damage"):
$hud/damage.text = String(val)
func set_enemy(val):
enemy = val
if enemy:
modulate = Color(1.0, 0.3, 0.3)
else:
modulate = Color(1.0, 1.0, 1.0)
func set_from(other):
set_move(other.move)
set_hull(other.hull)
set_shield(other.shield)
set_damage(other.damage)
set_enemy(other.enemy)