Skip to content

Commit

Permalink
Merge pull request #14 from T4g1/polish
Browse files Browse the repository at this point in the history
Polish
  • Loading branch information
T4g1 authored Sep 16, 2019
2 parents 465bceb + 7e308ee commit 2736f97
Show file tree
Hide file tree
Showing 21 changed files with 247 additions and 84 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export_presets.cfg

*.orig

build/
Binary file removed icon.png
Binary file not shown.
4 changes: 2 additions & 2 deletions main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ func _on_play():

$Game.start_game()

func _on_game_over(score):
func _on_game_over(score, reason):
var is_highest = score > highest
if is_highest:
highest = score
$Introduction.set_highest(score)

$GameOver.display(score, is_highest)
$GameOver.display(reason, score, is_highest)

func _on_popup_hide():
$Introduction.visible = true
Expand Down
27 changes: 16 additions & 11 deletions main.tscn
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
[gd_scene load_steps=6 format=2]
[gd_scene load_steps=7 format=2]

[ext_resource path="res://main.gd" type="Script" id=1]
[ext_resource path="res://assets/sea-bg.png" type="Texture" id=2]
[ext_resource path="res://scenes/Introduction/introduction.tscn" type="PackedScene" id=3]
[ext_resource path="res://scenes/Game/game.tscn" type="PackedScene" id=4]
[ext_resource path="res://scenes/GameOver/GameOver.tscn" type="PackedScene" id=5]
[ext_resource path="res://shader/sea-bg.tres" type="Material" id=2]
[ext_resource path="res://assets/sea-bg.png" type="Texture" id=3]
[ext_resource path="res://scenes/Introduction/introduction.tscn" type="PackedScene" id=4]
[ext_resource path="res://scenes/Game/game.tscn" type="PackedScene" id=5]
[ext_resource path="res://scenes/GameOver/GameOver.tscn" type="PackedScene" id=6]

[node name="Menus" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
mouse_filter = 2
script = ExtResource( 1 )

[node name="ColorRect" type="TextureRect" parent="."]
[node name="Background" type="TextureRect" parent="."]
material = ExtResource( 2 )
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = -69.0
margin_top = -56.0
margin_right = 117.0
margin_bottom = 76.0
mouse_filter = 2
texture = ExtResource( 2 )
texture = ExtResource( 3 )
stretch_mode = 2

[node name="Introduction" parent="." instance=ExtResource( 3 )]
[node name="Introduction" parent="." instance=ExtResource( 4 )]
mouse_filter = 2

[node name="Game" parent="." instance=ExtResource( 4 )]
[node name="Game" parent="." instance=ExtResource( 5 )]
visible = false
mouse_filter = 2

[node name="GameOver" parent="." instance=ExtResource( 5 )]
visible = false
[node name="GameOver" parent="." instance=ExtResource( 6 )]
[connection signal="play" from="Introduction" to="." method="_on_play"]
[connection signal="game_over" from="Game" to="." method="_on_game_over"]
[connection signal="confirmed" from="GameOver" to="." method="_on_popup_hide"]
42 changes: 31 additions & 11 deletions nodes/City/City.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ func show_ghost(polymino):
var city_size = $Grid.grid_size * $Grid.cell_size
var polymino_size = polymino.TETROMINO_SIZE * $Grid.cell_size
var offset = $Grid.rect_position - (city_size / 2) + (polymino_size / 2)
var cell_position = get_cell_position(polymino)

# Construct ghost shape from given tetromino
for x in range(polymino.TETROMINO_SIZE):
for y in range(polymino.TETROMINO_SIZE):
$Ghost.tiles[y][x] = min(1, polymino.tiles[y][x] + 1) * 8

$Ghost.visible = true
$Ghost.set_shape(polymino.shape)
$Ghost.position = offset + (get_cell_position(polymino) * $Grid.cell_size)
$Ghost.set_content($Ghost.tiles)
$Ghost.position = offset + (cell_position * $Grid.cell_size)

polymino.modulate = BARELY_VISIBLE

if is_valid_placement(polymino):
if is_valid_placement(polymino, cell_position):
$Ghost.modulate = GREEN
else:
$Ghost.modulate = RED
Expand All @@ -41,9 +47,8 @@ func hide_ghost():
# Merge the given polymino with the player city
# Return false on failed merge and does not modify anything in that case
func merge(polymino: Polymino):
if is_valid_placement(polymino):
var cell_position = get_cell_position(polymino)

var cell_position = get_cell_position(polymino)
if is_valid_placement(polymino, cell_position):
for x in range(polymino.TETROMINO_SIZE):
for y in range(polymino.TETROMINO_SIZE):
var tile = polymino.get_node("Grid").get_cell(x, y)
Expand All @@ -60,10 +65,25 @@ func merge(polymino: Polymino):
else:
return false

func is_valid_placement(polymino):
var cell_position = get_cell_position(polymino)

# Check placement is empty
func can_be_placed(polymino : Polymino):
var placement_possible = false

# For every rotation
for __ in range(4):
# Check every cell position possible for valid placement
for x in range($Grid.grid_size):# - polymino.TETROMINO_SIZE + 1):
for y in range($Grid.grid_size):# - polymino.TETROMINO_SIZE + 1):
var cell_position = Vector2(x, y)

if is_valid_placement(polymino, cell_position):
placement_possible = true

polymino.clockwise_rotate()

return placement_possible

func is_valid_placement(polymino, cell_position):
# Check placement position is empty
var check_pile : Array = []
var is_empty = true
for x in range(polymino.TETROMINO_SIZE):
Expand All @@ -85,7 +105,7 @@ func is_valid_placement(polymino):
is_empty = false
break

# Check one adjacent tile is not empty
# Check one adjacent tile is not empty so it connects with the island
var is_adjacent = false
for position in check_pile:
if !$Grid.is_free(position.x, position.y):
Expand Down
1 change: 1 addition & 0 deletions nodes/City/City.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ script = ExtResource( 1 )

[node name="Ghost" parent="." instance=ExtResource( 3 )]
visible = false
shape = 0
ghost = true

[node name="Blocs" type="Node2D" parent="."]
7 changes: 2 additions & 5 deletions nodes/Grid/Grid.gd
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ func flush():
$Content.clear()

# Takes an array and copies it into the current grid
func set_content(array, size, values):
func set_content(array, size):
$Content.clear()

var v_index := 0
for x in range(size):
for y in range(size):
if array[y][x]:
$Content.set_cell(x, y, values[v_index])
v_index += 1
$Content.set_cell(x, y, array[y][x])

# Compute how many cells are occupied
func get_size():
Expand Down
5 changes: 4 additions & 1 deletion nodes/Polymino/Dragable.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extends KinematicBody2D

signal clicked
signal double_clicked

var held = false

Expand All @@ -12,7 +13,9 @@ func _ready():

func _on_input_event(_viewport, event, _shape_idx):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
if event.doubleclick:
emit_signal("double_clicked")
elif event.button_index == BUTTON_LEFT and event.pressed:
emit_signal("clicked")

func _physics_process(_delta):
Expand Down
101 changes: 68 additions & 33 deletions nodes/Polymino/Polymino.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,36 @@ class_name Polymino

signal clicked

const EMPTY_TILE = 8
const TETROMINO_SIZE = 4
const PATTERN = [
[[1, 1, 0, 0], # Z
[0, 1, 1, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[0, 1, 1, 0], # S
[[0, 0, 0, 0], # Z
[1, 1, 0, 0],
[0, 0, 0, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]],
[[1, 0, 0, 0], # L
[1, 0, 0, 0],
[[0, 0, 0, 0], # S
[0, 1, 1, 0],
[1, 1, 0, 0],
[0, 0, 0, 0]],
[[0, 1, 0, 0], # J
[[0, 1, 0, 0], # L
[0, 1, 0, 0],
[1, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]],
[[1, 1, 0, 0], # O
[1, 1, 0, 0],
[0, 0, 0, 0],
[[0, 0, 1, 0], # J
[0, 0, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0], # O
[0, 1, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]],
[[1, 0, 0, 0], # I
[1, 0, 0, 0],
[1, 0, 0, 0],
[1, 0, 0, 0]],
[[0, 1, 0, 0], # W
[[0, 1, 0, 0], # I
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0]],
[[0, 0, 0, 0], # W
[0, 1, 0, 0],
[1, 1, 1, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
]

Expand All @@ -43,30 +44,64 @@ export(bool) var ghost := false
var bloc_packed: PackedScene = preload("res://nodes/Bloc/Bloc.tscn")

var blocs: Array = []
var tiles: Array = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]

func set_shape(new_shape):
if !has_node("Grid"):
return

for bloc in blocs:
bloc.free()

blocs.clear()

shape = new_shape
var tiles := []

for __ in range(0, TETROMINO_SIZE):
# Create Bloc from prefab
var bloc: Bloc = bloc_packed.instance()
var type := randi() % ResourceType.Types.size()
bloc.prod_type = type
blocs.push_front(bloc)
if !ghost:
tiles.push_front(ResourceType.TYPES_TILES_ID[type])
else:
tiles.push_front(8)

$Grid.set_content(PATTERN[shape], TETROMINO_SIZE, tiles)

# Generate an array with correct value for the tetromino
tiles = PATTERN[shape].duplicate(true)
for x in range(TETROMINO_SIZE):
for y in range(TETROMINO_SIZE):
if tiles[y][x]:
# Create Bloc from prefab
var bloc: Bloc = bloc_packed.instance()
var type := randi() % ResourceType.Types.size()
bloc.prod_type = type
blocs.push_front(bloc)

tiles[y][x] = EMPTY_TILE
if !ghost:
tiles[y][x] = ResourceType.TYPES_TILES_ID[type]
else:
tiles[y][x] = -1

set_content(tiles)

func set_content(new_tiles):
tiles = new_tiles.duplicate(true)

$Grid.set_content(tiles, TETROMINO_SIZE)

# Rotate 90 degrees clockwise
func clockwise_rotate():
var old_tiles = tiles.duplicate(true)

for x in range(TETROMINO_SIZE):
for y in range(TETROMINO_SIZE):
tiles[x][TETROMINO_SIZE - 1 - y] = old_tiles[y][x]

set_content(tiles)

func _on_clicked():
emit_signal("clicked", self)

func _on_double_clicked():
clockwise_rotate()

func get_blocs() -> Array:
return blocs

Expand Down
2 changes: 2 additions & 0 deletions nodes/Polymino/Polymino.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ script = ExtResource( 1 )
__meta__ = {
"_edit_group_": true
}
shape = 1

[node name="Grid" parent="." instance=ExtResource( 2 )]
grid_size = 4
Expand All @@ -26,4 +27,5 @@ script = ExtResource( 3 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="Dragable"]
shape = SubResource( 1 )
[connection signal="clicked" from="Dragable" to="." method="_on_clicked"]
[connection signal="double_clicked" from="Dragable" to="." method="_on_double_clicked"]
[connection signal="input_event" from="Dragable" to="Dragable" method="_on_input_event"]
1 change: 0 additions & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ _global_script_class_icons={

config/name="Merge Cities"
run/main_scene="res://main.tscn"
config/icon="res://icon.png"

[rendering]

Expand Down
Loading

0 comments on commit 2736f97

Please sign in to comment.