Skip to content

Commit

Permalink
add artificial intelligence and medkit
Browse files Browse the repository at this point in the history
  • Loading branch information
lbovet committed Aug 16, 2021
1 parent bd5a83c commit d1d9958
Show file tree
Hide file tree
Showing 18 changed files with 235 additions and 41 deletions.
15 changes: 10 additions & 5 deletions Aircraft.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ func drop():
box.apply_central_impulse(-transform.y * 500)
box.rotation = rotation + rand_range(-PI / 2, PI / 2)
var rnd = randf()
if rnd < 0.5:
box.setContent(1)
elif rnd < 0.9:
box.setContent(2)
if rnd > 0.8:
box.setContent(4) # health
elif rnd > 0.7:
box.setContent(3) # spring
elif rnd > 0.5:
box.setContent(2) # big
elif rnd > 0.2:
box.setContent(1) # small
else:
box.setContent(3)
box.setContent(0) # shell

box.z_index = z_index - 1
box.collision_layer = 128
box.collision_mask = 128
Expand Down
11 changes: 9 additions & 2 deletions Box.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extends RigidBody2D

export (PackedScene) var Shell

enum Content { SHELL=0, SMALL_SHELLS=1, BIG_SHELL=2, SPRING=3 }
enum Content { SHELL=0, SMALL_SHELLS=1, BIG_SHELL=2, SPRING=3, HEALTH=4 }

var content = null
var targetScale = Vector2(1, 1)
Expand All @@ -26,11 +26,16 @@ func _physics_process(delta):
func setContent(content):
$Sprite/Content.frame = content
self.content = content
if content == Content.SHELL:
shells = 15
elif content == Content.HEALTH:
$Sprite/Content.rotation = 0
life = 30

func hit(damage, source):
life -= damage
if life < 0:
if content != Content.SPRING:
if content != Content.SPRING and content != Content.HEALTH:
fire(source)
queue_free()

Expand Down Expand Up @@ -62,6 +67,8 @@ func _on_PickArea_body_entered(body):
body.addBigShell()
if content == Content.SMALL_SHELLS:
body.addSmallShells()
if content == Content.HEALTH:
body.addLife(0.6)

func _on_FallTimer_timeout():
z_index = 1
Expand Down
9 changes: 5 additions & 4 deletions Box.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=10 format=2]
[gd_scene load_steps=11 format=2]

[ext_resource path="res://images/box.png" type="Texture" id=1]
[ext_resource path="res://images/shell.png" type="Texture" id=2]
Expand All @@ -7,16 +7,17 @@
[ext_resource path="res://images/big-shell.png" type="Texture" id=5]
[ext_resource path="res://Box.gd" type="Script" id=6]
[ext_resource path="res://Shell.tscn" type="PackedScene" id=7]
[ext_resource path="res://images/health.png" type="Texture" id=8]

[sub_resource type="SpriteFrames" id=1]
animations = [ {
"frames": [ ExtResource( 2 ), ExtResource( 3 ), ExtResource( 5 ), ExtResource( 4 ) ],
"frames": [ ExtResource( 2 ), ExtResource( 3 ), ExtResource( 5 ), ExtResource( 4 ), ExtResource( 8 ) ],
"loop": true,
"name": "default",
"speed": 5.0
} ]

[sub_resource type="CapsuleShape2D" id=3]
[sub_resource type="CapsuleShape2D" id=2]
radius = 10.7314
height = 44.8859

Expand Down Expand Up @@ -48,7 +49,7 @@ frames = SubResource( 1 )

[node name="CollisionShape2D" type="CollisionShape2D" parent="PickArea"]
scale = Vector2( 2.06014, -0.801646 )
shape = SubResource( 3 )
shape = SubResource( 2 )

[node name="FallTimer" type="Timer" parent="."]
wait_time = 0.6
Expand Down
43 changes: 35 additions & 8 deletions Field.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,42 @@ signal tank_removed(tank)
signal game_over

var candidates
var state = [0,0,0]

func _ready():
candidates = [ $Tank1, $Tank2, $Tank3 ]
$Tank1.setOthers([$Tank2, $Tank3])
$Tank2.setOthers([$Tank1, $Tank3])
$Tank3.setOthers([$Tank2, $Tank1])
$Tank1.robot = true
$Tank2.robot = true

var winner = null

func update():
if len(candidates) < 2:
if len(candidates) == 1:
if len(candidates) == 1 and not candidates[0].robot:
winner = candidates[0]
$Airfcrafts.stop()
emit_signal("game_over")
else:
var zeroes=0
var maybeWinner = null
var humans=0
for c in candidates:
if c == null:
if c == null or c.robot:
continue
if c.points == 0:
if c.points <= 0:
zeroes += 1
else:
maybeWinner = c
if zeroes > len(candidates) -1:
humans += 1
if zeroes > humans-1:
winner = maybeWinner
$Airfcrafts.stop()
emit_signal("game_over")

func _on_Tank1_dead():
candidates.erase($Tank1)
state[0] = $Tank1.type
emit_signal("tank_removed", 1)
update()

Expand All @@ -48,6 +50,7 @@ func _on_Tank1_zero():

func _on_Tank2_dead():
candidates.erase($Tank2)
state[1] = $Tank2.type
emit_signal("tank_removed", 2)
update()

Expand All @@ -56,6 +59,7 @@ func _on_Tank2_zero():

func _on_Tank3_dead():
candidates.erase($Tank3)
state[2] = $Tank3.type
emit_signal("tank_removed", 3)
update()

Expand All @@ -64,8 +68,7 @@ func _on_Tank3_zero():

func spawnAircraft():
var spawn_location
if randf() > 0.5:

if randf() > 0.5:
spawn_location = $Airfcrafts/UpperAircraftSpawner/PathFollow2D
else:
spawn_location = $Airfcrafts/LowerAircraftSpawner/PathFollow2D
Expand Down Expand Up @@ -95,3 +98,27 @@ func _on_PathCalculation_paths_updated(paths):
$Tank2.updatePaths(paths)
if $Tank3 != null:
$Tank3.updatePaths(paths)

func getState():
return [
$Tank1.type if $Tank1 != null else state[0],
$Tank2.type if $Tank2 != null else state[1],
$Tank3.type if $Tank3 != null else state[2]
]

func setState(state):
$Tank1.setType(state[0])
$Tank2.setType(state[1])
$Tank3.setType(state[2])

func _on_Tank1_robot():
state[0] = $Tank1.type
emit_signal("tank_removed", 1)

func _on_Tank2_robot():
state[1] = $Tank2.type
emit_signal("tank_removed", 2)

func _on_Tank3_robot():
state[2] = $Tank3.type
emit_signal("tank_removed", 3)
5 changes: 4 additions & 1 deletion Field.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ position = Vector2( 1035.76, 300.097 )
shape = SubResource( 4 )

[node name="Airfcrafts" type="Timer" parent="."]
wait_time = 20.0
wait_time = 14.0
autostart = true

[node name="UpperAircraftSpawner" type="Path2D" parent="Airfcrafts"]
Expand All @@ -102,9 +102,12 @@ position = Vector2( 736.224, 683.747 )
rotation = 3.13704

[connection signal="dead" from="Tank1" to="." method="_on_Tank1_dead"]
[connection signal="robot" from="Tank1" to="." method="_on_Tank1_robot"]
[connection signal="zero" from="Tank1" to="." method="_on_Tank1_zero"]
[connection signal="dead" from="Tank2" to="." method="_on_Tank2_dead"]
[connection signal="robot" from="Tank2" to="." method="_on_Tank2_robot"]
[connection signal="zero" from="Tank2" to="." method="_on_Tank2_zero"]
[connection signal="dead" from="Tank3" to="." method="_on_Tank3_dead"]
[connection signal="robot" from="Tank3" to="." method="_on_Tank3_robot"]
[connection signal="zero" from="Tank3" to="." method="_on_Tank3_zero"]
[connection signal="timeout" from="Airfcrafts" to="." method="_on_Airfcrafts_timeout"]
18 changes: 17 additions & 1 deletion Game.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extends Node2D

var gameOver = false
var currentLevel
var clean = true

func _ready():
var level = randi() % $Levels.get_children().size()
Expand All @@ -13,25 +14,40 @@ func _ready():
$GameOver.hide()
if not OS.has_touchscreen_ui_hint():
$Controls.queue_free()

var save_game = File.new()
if save_game.file_exists("user://savegame.save"):
save_game.open("user://savegame.save", File.READ)
var data = parse_json(save_game.get_line())
$Field.setState(data.players)
save_game.close()

func _process(delta):
if Input.is_action_just_pressed("reset"):
if gameOver:
$Camera2D.make_current()
$GameOver.hide()
save($Field.getState())
get_tree().reload_current_scene()

func _on_Field_game_over():
save($Field.getState())
$GameOver/Timer.start()
if $Field.winner != null:
$Field.winner.focus()

func save(state):
var save_game = File.new()
save_game.open("user://savegame.save", File.WRITE)
save_game.store_line(to_json({ "players": state}))
save_game.close()

func _on_Timer_timeout():
gameOver = true
if $Field.winner == null:
$GameOver.show()

func _on_Field_tank_removed(tank):
clean = false
if $Controls == null:
return
if(tank == 1):
Expand Down
11 changes: 6 additions & 5 deletions Game.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,32 @@ rocks = 0.9

[node name="Controls1" parent="Controls" instance=ExtResource( 4 )]
modulate = Color( 0.635294, 0.588235, 0.0313726, 0.360784 )
position = Vector2( 103.847, 46.9703 )
position = Vector2( 103.847, 57.9703 )
rotation = 1.5708
player = 1

[node name="Controls2" parent="Controls" instance=ExtResource( 4 )]
modulate = Color( 0.054902, 0.435294, 0.835294, 0.388235 )
position = Vector2( 917.263, 533.288 )
position = Vector2( 917.263, 537.288 )
rotation = -1.5708
player = 2

[node name="Controls3" parent="Controls" instance=ExtResource( 4 )]
modulate = Color( 0.603922, 0.247059, 0.803922, 0.356863 )
position = Vector2( 393.722, 496.634 )
position = Vector2( 395.722, 496.634 )
player = 3

[node name="GameOver" type="Node2D" parent="."]
z_index = 156
z_as_relative = false

[node name="Label" type="Label" parent="GameOver"]
margin_left = 205.407
margin_top = 237.133
margin_right = 797.406
margin_bottom = 357.133
custom_fonts/font = SubResource( 1 )
text = "No Winner"
text = "Game Over"
align = 1
valign = 1
__meta__ = {
Expand Down Expand Up @@ -142,7 +144,6 @@ z_as_relative = false
script = ExtResource( 6 )

[node name="PathUpdate" type="Timer" parent="ArtificialIntelligence/PathCalculation"]
wait_time = 0.5

[connection signal="ready" from="." to="ArtificialIntelligence/PathCalculation" method="_on_Game_ready"]
[connection signal="game_over" from="Field" to="." method="_on_Field_game_over"]
Expand Down
22 changes: 18 additions & 4 deletions PathCalculation.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const DEBUG_PATHS = false
var rocks
var triangles
var ready = false
var blacklist
var paths
var thread

signal paths_updated(paths)

Expand All @@ -19,6 +21,10 @@ func _on_Game_ready():
$PathUpdate.start()

func _draw():
if DEBUG_PATHS:
calculate()

func calculate(ignore=null):
if not ready:
return
if DEBUG_PATHS:
Expand All @@ -28,7 +34,7 @@ func _draw():
draw_circle(to_local(c), 6, Color(1,0,0,0.5))
var threshold = 80
paths = []
var blacklist = []
blacklist = []
for t in triangles:
var path = []
for i in range(0,3):
Expand All @@ -43,15 +49,15 @@ func _draw():
path.append(center)
if len(path) > 2:
path.append(path[0])
paths.append(path)
paths.append(path)
for path in paths:
var toRemove=[]
for point in blacklist:
for i in range(len(path)-1,-1, -1):
if (point - path[i]).length() < 10:
path.remove(i)
if len(path) == 1:
path.remove(0)
path.remove(0)
for path in paths:
for i in range(len(path)-1, -1, -1):
var n = 0
Expand Down Expand Up @@ -90,7 +96,6 @@ func triangulate():
for triple in triangleArray:
triangles.append([allPoints[triple[0]], allPoints[triple[1]], allPoints[triple[2]]])
ready = true
update()

func getCorners():
var node = get_parent().get_parent().get_node("Field/CollisionShape2D")
Expand All @@ -104,3 +109,12 @@ func getCorners():

func _on_PathUpdate_timeout():
triangulate()
if DEBUG_PATHS:
update()
else:
thread = Thread.new()
thread.start(self, "calculate", null)

func _exit_tree():
if thread != null:
thread.wait_to_finish()
Loading

0 comments on commit d1d9958

Please sign in to comment.