Skip to content

Commit

Permalink
Refine code (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-- authored Nov 7, 2021
1 parent 5912fac commit ef28fdc
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The tricky part can be steering, because you steer the car as a driver (like in

![main](screenshots/main.png)

![taxi](screenshots/taxi.png)
![ambulance](screenshots/ambulance.png)

![truck](screenshots/truck.png)

Expand Down
Binary file added assets/city/heart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://src/city.gd"
}, {
"base": "Sprite",
"class": "Heart",
"language": "GDScript",
"path": "res://src/heart.gd"
}, {
"base": "MarginContainer",
"class": "Main",
"language": "GDScript",
Expand All @@ -32,6 +37,7 @@ _global_script_classes=[ {
_global_script_class_icons={
"Auto": "",
"City": "",
"Heart": "",
"Main": "",
"Play": ""
}
Expand Down
Binary file added screenshots/ambulance.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed screenshots/taxi.png
Binary file not shown.
Binary file modified screenshots/truck.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions src/auto.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ class_name Auto
extends AnimatedSprite
# Generic implementation for all vehicles.

signal on_move(pos)

const INTERPOLATE_DURATION := 1.0
const MIN_INTERPOLATE_DURATION := 0.49
const INTERPOLATE_TRANSITION := Tween.TRANS_LINEAR
const INTERPOLATE_EASE := Tween.EASE_OUT_IN

const OFFSET := Vector2(0, 32)

const Dir2Anim: Dictionary = {
Vector2.UP: "n",
Vector2.DOWN: "s",
Expand All @@ -27,7 +31,6 @@ onready var _tween: Tween = $Tween
func _ready():
set_sprite_frames(load(Global.auto_sprite))


func move():
play(Dir2Anim[_dir])

Expand All @@ -39,7 +42,7 @@ func move():
_dt -= get_process_delta_time()

_pos += _dir
var world_pos = map_to_world.call_func(_pos) as Vector2 if map_to_world != null else _pos
var world_pos = map_to_world.call_func(_pos, OFFSET) as Vector2 if map_to_world != null else _pos
if not _tween.interpolate_property(self, "position", null, world_pos, _dt, INTERPOLATE_TRANSITION, INTERPOLATE_EASE):
printerr("interpolate_property", world_pos, _dt, INTERPOLATE_TRANSITION, INTERPOLATE_EASE)
if not _tween.start():
Expand All @@ -66,8 +69,11 @@ func turn_right():
func turn_u():
_dir = Vector2(-_dir.x, -_dir.y)

func map_position() -> Vector2:
return _pos

func _on_Tween_completed(_object, _key):
if not _is_moving:
return
emit_signal("on_move", _pos)
move()
35 changes: 27 additions & 8 deletions src/city.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const Val2Dir: Dictionary = {

const N_TILE_TYPES = 16

export(Vector2) var auto_offset: Vector2 = Vector2(0, 32)
var Heart = preload("res://src/heart.tscn")
var _hearts: Dictionary = {}

var _n_exits: int = 0
onready var _map: TileMap = $TileMap
Expand All @@ -44,17 +45,17 @@ func _ready():
and (_map.get_cellv(v + Val2Dir[val]) == TileMap.INVALID_CELL)):
_n_exits += 1

func map_to_world(pos: Vector2) -> Vector2:
return _map.map_to_world(pos) + auto_offset
func map_to_world(pos: Vector2, off: Vector2) -> Vector2:
return _map.map_to_world(pos) + off

func world_to_map(pos: Vector2) -> Vector2:
return _map.world_to_map(pos - auto_offset)
func world_to_map(pos: Vector2, off: Vector2) -> Vector2:
return _map.world_to_map(pos - off)

func get_or_set_tile(pos: Vector2, dir: Vector2) -> int:
var n_exits: int = _n_exits
var tile_pos: Vector2 = pos + dir
var tile_idx: int = _map.get_cellv(tile_pos)
var tile_exists: bool = (tile_idx != TileMap.INVALID_CELL)
var n_exits: int = _n_exits
if not tile_exists:
var rand_tile := _rand_tile(tile_pos)
tile_idx = rand_tile[0]
Expand All @@ -70,8 +71,26 @@ func get_or_set_tile(pos: Vector2, dir: Vector2) -> int:
_n_exits = n_exits
return tile_idx


func has_tile(pos: Vector2, dir: Vector2) -> bool:
func add_heart(exclude_pos: Array):
var cells: Array = _map.get_used_cells()
var pos: Vector2 = cells[randi() % len(cells)]
if _hearts.has(pos) or (exclude_pos.find(pos) != -1):
return
var heart = Heart.instance()
heart.position = map_to_world(pos, heart.OFFSET)
_hearts[pos] = heart
add_child(heart)

func delete_heart(pos: Vector2) -> bool:
var heart: Heart = _hearts.get(pos)
if heart == null:
return false
heart.queue_free()
if not _hearts.erase(pos):
printerr(_hearts, "erase", pos)
return true

func has_road(pos: Vector2, dir: Vector2) -> bool:
return get_or_set_tile(pos, dir) > 0

func has_exits() -> bool:
Expand Down
9 changes: 2 additions & 7 deletions src/control.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ signal on_control_right
signal on_control_up
signal on_control_down
signal on_control_cancel
signal on_control_quit

const MaxDiag: float = 1.4

Expand All @@ -19,7 +20,7 @@ func _ready():
func _input(event: InputEvent):
if _is_stopped:
if (event is InputEventKey) or (event is InputEventScreenTouch) or (event is InputEventMouseButton):
quit()
emit_signal("on_control_quit")
return

if (event is InputEventKey):
Expand Down Expand Up @@ -69,12 +70,6 @@ func _input_screen_touch(event: InputEventScreenTouch):
func stop():
_is_stopped = true

func quit():
queue_free()
var err := get_tree().change_scene("res://src/main.tscn")
if err != OK:
printerr("change_scene", "res://src/main.tscn", err)


func _on_LeftButton_pressed():
emit_signal("on_control_left")
Expand Down
7 changes: 7 additions & 0 deletions src/heart.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class_name Heart
extends Sprite

const OFFSET := Vector2(0, 14)

func _process(delta):
rotate(delta)
8 changes: 8 additions & 0 deletions src/heart.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_scene load_steps=3 format=2]

[ext_resource path="res://assets/city/heart.png" type="Texture" id=1]
[ext_resource path="res://src/heart.gd" type="Script" id=2]

[node name="Heart" type="Sprite"]
texture = ExtResource( 1 )
script = ExtResource( 2 )
1 change: 1 addition & 0 deletions src/main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ margin_bottom = 113.0
custom_fonts/font = SubResource( 2 )
custom_colors/font_color = Color( 0.627451, 0.627451, 0.627451, 1 )
text = "0"

[connection signal="pressed" from="VBoxContainer/LogoContainer/TouchButton" to="." method="_on_TouchButton_pressed"]
[connection signal="pressed" from="VBoxContainer/ButtonContainer/TaxiButton" to="." method="_on_TaxiButton_pressed"]
[connection signal="pressed" from="VBoxContainer/ButtonContainer/TruckButton" to="." method="_on_TruckButton_pressed"]
Expand Down
51 changes: 31 additions & 20 deletions src/play.gd
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
class_name Play
extends Control


# Play timeout stands for number of seconds, we gonna play.
export(int) var timeout = 45
export(int) var timeout = 45
export(int) var interval = 10
export(int) var extra_time = 5

var _heart_interval: int = interval

onready var _timer: Timer = $Timer

Expand All @@ -17,14 +20,14 @@ onready var _city: City = $City
onready var _auto: Auto = $Auto

func _ready():
_auto.map_to_world = funcref(_city, "map_to_world")
_auto.can_move = funcref(_city, "has_tile")
_auto.position = _city.map_to_world(Vector2.ZERO)
Global.play_score = 0
_score_label.text = str(Global.play_score)
_timer_label.text = str(timeout)
_timer.set_paused(true)
_timer.start(-1.0)
_auto.map_to_world = funcref(_city, "map_to_world")
_auto.can_move = funcref(_city, "has_road")
_auto.position = _city.map_to_world(Vector2.ZERO, _auto.OFFSET)

func _on_Control_left():
_timer.set_paused(false)
Expand All @@ -49,32 +52,40 @@ func _on_Control_cancel():
_auto.stop()
_timer.set_paused(true)

func _on_Control_quit():
queue_free()
var err := get_tree().change_scene("res://src/main.tscn")
if err != OK:
printerr("change_scene", "res://src/main.tscn", err)

func _on_City_tile_set(_pos: Vector2, _idx: int):
Global.play_score += 1
_score_label.text = str(Global.play_score)

func _on_Auto_move(map_pos: Vector2):
if _city.delete_heart(map_pos):
var t: int = int(_timer_label.text) + extra_time
_timer_label.text = str(t)

func _on_Timer_timeout():
var t := int(_timer_label.text) - 1
var t: int = int(_timer_label.text) - 1
_timer_label.text = str(t)
if (t == 0) or (not _city.has_exits()):
stop()
_timer_label.set("custom_colors/font_color",Color.lightcoral)
_score_label.set("custom_colors/font_color",Color.lightsteelblue)
if Global.play_best_score < Global.play_score:
Global.play_best_score = Global.play_score
Global.play_best_auto_icon = Global.auto_icon

return
_heart_interval -= 1
if _heart_interval == 0:
_heart_interval = interval
_city.add_heart([_auto.map_position()])

func stop():
_auto.stop()
_auto.disconnect("on_move", self, "_on_Auto_move")
_control.stop()
_timer.stop()
_city.disconnect("tile_set", self, "_on_City_tile_set")


func quit():
queue_free()
var err := get_tree().change_scene("res://src/main.tscn")
if err != OK:
printerr("change_scene", "res://src/main.tscn", err)

_timer_label.set("custom_colors/font_color",Color.lightcoral)
_score_label.set("custom_colors/font_color",Color.lightsteelblue)
if Global.play_best_score < Global.play_score:
Global.play_best_score = Global.play_score
Global.play_best_auto_icon = Global.auto_icon
3 changes: 3 additions & 0 deletions src/play.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,18 @@ drag_margin_left = 0.3
drag_margin_top = 0.3
drag_margin_right = 0.3
drag_margin_bottom = 0.3

[connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"]
[connection signal="on_control_cancel" from="CanvasLayer/Control" to="." method="_on_Control_cancel"]
[connection signal="on_control_down" from="CanvasLayer/Control" to="." method="_on_Control_down"]
[connection signal="on_control_left" from="CanvasLayer/Control" to="." method="_on_Control_left"]
[connection signal="on_control_quit" from="CanvasLayer/Control" to="." method="_on_Control_quit"]
[connection signal="on_control_right" from="CanvasLayer/Control" to="." method="_on_Control_right"]
[connection signal="on_control_up" from="CanvasLayer/Control" to="." method="_on_Control_up"]
[connection signal="pressed" from="CanvasLayer/Control/UpDownContainer/UpButton" to="CanvasLayer/Control" method="_on_UpButton_pressed"]
[connection signal="pressed" from="CanvasLayer/Control/UpDownContainer/DownButton" to="CanvasLayer/Control" method="_on_DownButton_pressed"]
[connection signal="pressed" from="CanvasLayer/Control/LeftRightContainer/HBoxContainer/LeftButton" to="CanvasLayer/Control" method="_on_LeftButton_pressed"]
[connection signal="pressed" from="CanvasLayer/Control/LeftRightContainer/HBoxContainer/RightButton" to="CanvasLayer/Control" method="_on_RightButton_pressed"]
[connection signal="tile_set" from="City" to="." method="_on_City_tile_set"]
[connection signal="on_move" from="Auto" to="." method="_on_Auto_move"]
[connection signal="tween_completed" from="Auto/Tween" to="Auto" method="_on_Tween_completed"]
9 changes: 8 additions & 1 deletion test/test_city.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=5 format=2]

[ext_resource path="res://src/city.tres" type="TileSet" id=1]
[ext_resource path="res://src/city.gd" type="Script" id=2]
[ext_resource path="res://src/heart.gd" type="Script" id=3]
[ext_resource path="res://assets/city/heart.png" type="Texture" id=4]

[node name="City" type="Node2D"]
script = ExtResource( 2 )
Expand All @@ -16,3 +18,8 @@ cell_size = Vector2( 128, 64 )
compatibility_mode = true
format = 1
tile_data = PoolIntArray( -131072, 4, 0, -65536, 7, 0, -65535, 12, 0, 65534, 2, 0, 65535, 10, 0, 0, 15, 0, 65536, 1, 0 )

[node name="Heart" type="Sprite" parent="."]
position = Vector2( 0, 13.4696 )
texture = ExtResource( 4 )
script = ExtResource( 3 )

0 comments on commit ef28fdc

Please sign in to comment.