Skip to content

Commit

Permalink
Add level select, dominoes, and reformat levels
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicgenius committed Oct 4, 2020
1 parent fae98ae commit 9315c4d
Show file tree
Hide file tree
Showing 30 changed files with 542 additions and 45 deletions.
Binary file modified Trains/assets/Tilemap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 15 additions & 10 deletions Trains/src/Mechanics.gd
Expand Up @@ -5,16 +5,20 @@ onready var tree = get_tree()
var is_moving: bool = false
var enemies: int = 0 setget set_enemies

var total_levels = 0
const LEVEL_PREFIX: String = "res://src/levels/Level_"
var max_levels = 12
const LEVEL_PREFIX: String = "res://src/levels/%s/Level_"

const LEVEL_SELECT: String = "res://src/ui/LevelSelect.tscn"
const WIN_SCREEN: String = "res://src/ui/WinScreen.tscn"

signal on_win

func _ready() -> void:
# check how many levels exist of the form level prefix + integer
var file = File.new()
while file.file_exists(LEVEL_PREFIX + str(total_levels + 1) + ".tscn"):
total_levels += 1
# check how many levels exist of the form level prefix + integer
# var file = File.new()
# while file.file_exists(LEVEL_PREFIX + str(total_levels + 1) + ".tscn"):
# total_levels += 1
pass

func set_enemies(value: int) -> void:
enemies = value
Expand All @@ -27,9 +31,10 @@ func set_enemies(value: int) -> void:
# goes to next level by getting the integer part of the current level scene and
# adding 1, then appending to the level prefix
func _go_to_next_level() -> void:
var current_level = int(tree.current_scene.name)
var current_level = int(tree.current_scene.filename)

# check if this isn't the last level
if current_level != total_levels:
tree.change_scene(LEVEL_PREFIX + str(current_level + 1) + ".tscn")
if current_level != max_levels:
tree.change_scene(LEVEL_PREFIX % tree.current_scene.filename.split("/")[4] + str(current_level + 1) + ".tscn")
else:
tree.change_scene("res://src/ui/WinScreen.tscn")
tree.change_scene(WIN_SCREEN)
45 changes: 29 additions & 16 deletions Trains/src/blocks/Block.gd
Expand Up @@ -13,6 +13,9 @@ const MOVE_ANIM_NAME: = ["right", "up", "left", "down"]

var move_dir_index: int = 0

var _self_block_pos: Array = [Vector2.ZERO]
var _targets: Array = []

# tries to move the block
# if works is true, then it actually moves
# otherwise, it just trembles
Expand All @@ -22,37 +25,47 @@ func try_move(works: bool) -> void:
else:
anim_player.play(TREMBLE_PREFIX + MOVE_ANIM_NAME[move_dir_index])

var next_block: Block = _block_toward(MOVE_DIR[move_dir_index] * size)
if next_block != null:
next_block.try_move(works)
for block in _targets:
block.try_move(works)

# sets the move dir index and returns if the block can move in that direction
func set_move_dir_index(new_index: int) -> bool:
move_dir_index = new_index

var next_block: Block = _block_toward(MOVE_DIR[new_index] * size)
if next_block != null:
return next_block.set_move_dir_index(new_index)
return true

func _block_toward(direction: Vector2) -> Block:
ray.cast_to = direction
ray.force_raycast_update()
_find_targets_toward(MOVE_DIR[new_index] * size)

var next_block_area_2d = ray.get_collider()
var can_move: bool = true
for block in _targets:
can_move = can_move and block.set_move_dir_index(new_index)

if next_block_area_2d != null:
return next_block_area_2d.get_parent()
return null;
return can_move

func _find_targets_toward(direction: Vector2) -> void:
_targets = []
for pos in _self_block_pos:
ray.cast_to = direction
ray.position = pos
ray.force_raycast_update()

var next_block_area_2d = ray.get_collider()

if next_block_area_2d != null:
_targets.append(next_block_area_2d.get_parent())


func _finish_movement(move_dir_index: int) -> void:
sprite.position = Vector2(0, 0)
self.position += MOVE_DIR[move_dir_index] * size;

if not self.get_parent().is_valid(position):
if not _is_valid_spot():
_die()
# print (Mechanics.enemies)

func _is_valid_spot() -> bool:
for pos in _self_block_pos:
if not self.get_parent().is_valid(position + pos):
return false
return true

func _die() -> void:
queue_free()
12 changes: 6 additions & 6 deletions Trains/src/blocks/Block.tscn
Expand Up @@ -138,7 +138,7 @@ tracks/0/keys = {
"values": [ Vector2( 0, 0 ) ]
}

[sub_resource type="Animation" id=9]
[sub_resource type="Animation" id=6]
resource_name = "tremble_down"
length = 0.2
step = 0.025
Expand Down Expand Up @@ -189,7 +189,7 @@ tracks/0/keys = {
"values": [ Vector2( 0, 0 ), Vector2( 0.5, 0 ), Vector2( 0, 0 ), Vector2( -0.5, 0 ), Vector2( 0, 0 ), Vector2( 0.5, 0 ), Vector2( 0, 0 ), Vector2( -0.5, 0 ), Vector2( 0, 0 ) ]
}

[sub_resource type="Animation" id=10]
[sub_resource type="Animation" id=9]
resource_name = "tremble_up"
length = 0.2
step = 0.025
Expand All @@ -206,7 +206,7 @@ tracks/0/keys = {
"values": [ Vector2( 0, 0 ), Vector2( 0, -0.5 ), Vector2( 0, 0 ), Vector2( 0, 0.5 ), Vector2( 0, 0 ), Vector2( 0, -0.5 ), Vector2( 0, 0 ), Vector2( 0, 0.5 ), Vector2( 0, 0 ) ]
}

[sub_resource type="RectangleShape2D" id=6]
[sub_resource type="RectangleShape2D" id=10]
extents = Vector2( 4, 4 )

[node name="Block" type="Node2D"]
Expand All @@ -223,17 +223,17 @@ anims/move_left = SubResource( 2 )
anims/move_right = SubResource( 3 )
anims/move_up = SubResource( 4 )
anims/start = SubResource( 5 )
anims/tremble_down = SubResource( 9 )
anims/tremble_down = SubResource( 6 )
anims/tremble_left = SubResource( 7 )
anims/tremble_right = SubResource( 8 )
anims/tremble_up = SubResource( 10 )
anims/tremble_up = SubResource( 9 )

[node name="Area2D" type="Area2D" parent="."]
collision_layer = 2
collision_mask = 2

[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
shape = SubResource( 6 )
shape = SubResource( 10 )

[node name="RayCast2D" type="RayCast2D" parent="Area2D"]
enabled = true
Expand Down
20 changes: 20 additions & 0 deletions Trains/src/blocks/Domino.gd
@@ -0,0 +1,20 @@
extends Block

var domino_direction: int = 1
onready var collision_shape = $Area2D/CollisionShape2D

func _ready() -> void:
if domino_direction == 0:
_self_block_pos = [Vector2(4, 0), Vector2(-4, 0)]
collision_shape.scale = Vector2(2, 1)
else:
_self_block_pos = [Vector2(0, 4), Vector2(0, -4)]
collision_shape.scale = Vector2(1, 2)

_set_sprite(domino_direction)

func _set_sprite(domino_direction: int) -> void:
if domino_direction == 0:
sprite.region_rect = Rect2(32, 0, 16, 8)
else:
sprite.region_rect = Rect2(24, 0, 8, 16)
10 changes: 10 additions & 0 deletions Trains/src/blocks/Domino.tscn
@@ -0,0 +1,10 @@
[gd_scene load_steps=3 format=2]

[ext_resource path="res://src/blocks/Block.tscn" type="PackedScene" id=1]
[ext_resource path="res://src/blocks/Domino.gd" type="Script" id=2]

[node name="Domino" instance=ExtResource( 1 )]
script = ExtResource( 2 )

[node name="sprite" parent="." index="0"]
region_rect = Rect2( 24, 0, 8, 16 )
Binary file added Trains/src/blocks/Tilemap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions Trains/src/blocks/Tilemap.png.import
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/Tilemap.png-85bbc710ad2855b7a5b91906b9d5d3b6.stex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://src/blocks/Tilemap.png"
dest_files=[ "res://.import/Tilemap.png-85bbc710ad2855b7a5b91906b9d5d3b6.stex" ]

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0
29 changes: 28 additions & 1 deletion Trains/src/blocks/block.tres
Expand Up @@ -3,7 +3,6 @@
[ext_resource path="res://assets/Tilemap.png" type="Texture" id=1]

[resource]
resource_local_to_scene = true
0/name = "Tilemap.png 0"
0/texture = ExtResource( 1 )
0/tex_offset = Vector2( 0, 0 )
Expand Down Expand Up @@ -116,3 +115,31 @@ resource_local_to_scene = true
7/shape_one_way_margin = 0.0
7/shapes = [ ]
7/z_index = 0
8/name = "Tilemap.png 8"
8/texture = ExtResource( 1 )
8/tex_offset = Vector2( 0, 0 )
8/modulate = Color( 1, 1, 1, 1 )
8/region = Rect2( 24, 0, 8, 16 )
8/tile_mode = 0
8/occluder_offset = Vector2( 0, 0 )
8/navigation_offset = Vector2( 0, 0 )
8/shape_offset = Vector2( 0, 0 )
8/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
8/shape_one_way = false
8/shape_one_way_margin = 0.0
8/shapes = [ ]
8/z_index = 0
9/name = "Tilemap.png 9"
9/texture = ExtResource( 1 )
9/tex_offset = Vector2( 0, 0 )
9/modulate = Color( 1, 1, 1, 1 )
9/region = Rect2( 32, 0, 16, 8 )
9/tile_mode = 0
9/occluder_offset = Vector2( 0, 0 )
9/navigation_offset = Vector2( 0, 0 )
9/shape_offset = Vector2( 0, 0 )
9/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
9/shape_one_way = false
9/shape_one_way_margin = 0.0
9/shapes = [ ]
9/z_index = 0
25 changes: 25 additions & 0 deletions Trains/src/levels/Blocks.gd
Expand Up @@ -4,6 +4,7 @@ const Block = preload("res://src/blocks/Block.tscn")
const Enemy = preload("res://src/blocks/Enemy.tscn")
const Train = preload("res://src/blocks/Train.tscn")
const Immovable = preload("res://src/blocks/Immovable.tscn")
const Domino = preload("res://src/blocks/Domino.tscn")

const OFFSET: Vector2 = Vector2(4, 4)

Expand All @@ -21,13 +22,22 @@ func _ready() -> void:

var immovable_blocks = get_used_cells_by_id(7)

var vertical_dominoes = get_used_cells_by_id(8)
var horizontal_dominoes = get_used_cells_by_id(9)

_replace_normal_blocks(normal_blocks)

_replace_trains(right_trains, 0)
_replace_trains(up_trains, 1)
_replace_trains(left_trains, 2)
_replace_trains(down_trains, 3)

_replace_enemies(enemies)

_replace_immovable_blocks(immovable_blocks)

_replace_dominoes(vertical_dominoes, 1)
_replace_dominoes(horizontal_dominoes, 0)

func _replace_normal_blocks(tileArr: Array) -> void:
for i in range(tileArr.size()):
Expand Down Expand Up @@ -61,5 +71,20 @@ func _replace_immovable_blocks(tileArr: Array) -> void:
set_cell(tileArr[i].x, tileArr[i].y, -1)
self.add_child(new_block)

func _replace_dominoes(tileArr: Array, domino_direction: int) -> void:
var OFFSET_CUSTOM = Vector2.ZERO

if domino_direction == 0:
OFFSET_CUSTOM = Vector2(4, 0)
else:
OFFSET_CUSTOM = Vector2(0, 4)

for i in range(tileArr.size()):
var new_block = Domino.instance()
new_block.position = OFFSET + OFFSET_CUSTOM + map_to_world(tileArr[i])
new_block.domino_direction = domino_direction
set_cell(tileArr[i].x, tileArr[i].y, -1)
self.add_child(new_block)

func is_valid(world_pos : Vector2) -> bool:
return get_cellv(world_to_map(world_pos)) != WATER
14 changes: 8 additions & 6 deletions Trains/src/levels/LevelTemplate.tscn
@@ -1,10 +1,11 @@
[gd_scene load_steps=6 format=2]
[gd_scene load_steps=7 format=2]

[ext_resource path="res://src/blocks/block.tres" type="TileSet" id=1]
[ext_resource path="res://src/ui/Restart.tscn" type="PackedScene" id=2]
[ext_resource path="res://src/ui/ui.tres" type="Theme" id=3]
[ext_resource path="res://src/levels/Blocks.gd" type="Script" id=4]
[ext_resource path="res://src/levels/LevelNumber.gd" type="Script" id=5]
[ext_resource path="res://src/ui/BackToLevelSelect.tscn" type="PackedScene" id=6]

[node name="LevelTemplate" type="Node2D"]

Expand All @@ -23,22 +24,23 @@ __meta__ = {
position = Vector2( 32, -32 )

[node name="TL" type="Control" parent="UI"]
margin_left = 8.0
margin_top = 8.0
margin_right = 8.0
margin_bottom = 8.0
theme = ExtResource( 3 )
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Label" type="Label" parent="UI/TL"]
margin_left = 62.0
margin_top = 14.0
text = "Level %s"
script = ExtResource( 5 )
__meta__ = {
"_edit_use_anchors_": false
}

[node name="BackToLevelSelect" parent="UI/TL" instance=ExtResource( 6 )]
position = Vector2( 32, 32 )

[node name="Game" type="CanvasLayer" parent="."]

[node name="Control" type="Control" parent="Game"]
Expand All @@ -54,5 +56,5 @@ scale = Vector2( 8, 8 )
tile_set = ExtResource( 1 )
cell_size = Vector2( 8, 8 )
format = 1
tile_data = PoolIntArray( 131069, 1, 0, 131070, 1, 0, 131071, 1, 0, 65536, 1, 0, 65537, 1, 0, 65538, 1, 0, 196605, 1, 0, 196606, 1, 0, 196607, 1, 0, 131072, 7, 0, 131073, 2, 0, 131074, 1, 0, 262141, 1, 0, 262142, 1, 0, 262143, 0, 0, 196609, 0, 0, 196610, 1, 0, 327677, 1, 0, 327678, 3, 0, 327679, 2, 0, 262144, 0, 0, 262145, 2, 0, 262146, 1, 0, 393213, 1, 0, 393214, 0, 0, 393215, 0, 0, 327680, 4, 0, 327681, 0, 0, 327682, 1, 0, 458749, 1, 0, 458750, 1, 0, 458751, 1, 0, 393216, 1, 0, 393217, 1, 0, 393218, 1, 0 )
tile_data = PoolIntArray( 131069, 1, 0, 131070, 1, 0, 131071, 1, 0, 65536, 1, 0, 65537, 1, 0, 65538, 1, 0, 196605, 1, 0, 196606, 1, 0, 196607, 1, 0, 131072, 7, 0, 131073, 2, 0, 131074, 1, 0, 262141, 1, 0, 262142, 1, 0, 262143, 0, 0, 196609, 0, 0, 196610, 1, 0, 327677, 1, 0, 327678, 3, 0, 327679, 2, 0, 262144, 0, 0, 262145, 2, 0, 262146, 1, 0, 393213, 1, 0, 393214, 0, 0, 393215, 0, 0, 327680, 4, 0, 327681, 0, 0, 327682, 1, 0, 458749, 1, 0, 458750, 1, 0, 458751, 1, 0, 393216, 1, 0, 393217, 1, 0, 393218, 1, 0, 524287, 0, 0, 458752, 0, 0, 458753, 8, 0, 458754, 0, 0, 589822, 3, 0, 589823, 9, 0, 524290, 0, 0, 589824, 4, 0, 589825, 4, 0 )
script = ExtResource( 4 )
Expand Up @@ -121,6 +121,7 @@ resource_local_to_scene = true
[node name="Level_1" instance=ExtResource( 1 )]

[node name="Label" type="Label" parent="UI/BL" index="1"]
visible = false
margin_left = 68.0
margin_top = -50.0
margin_right = 108.0
Expand All @@ -135,6 +136,7 @@ tile_set = SubResource( 1 )
tile_data = PoolIntArray( 131069, 1, 0, 131070, 1, 0, 131071, 1, 0, 65536, 1, 0, 65537, 1, 0, 65538, 1, 0, 196605, 1, 0, 196606, 3, 0, 196607, 0, 0, 131072, 2, 0, 131074, 1, 0, 262141, 1, 0, 262142, 1, 0, 262143, 1, 0, 196608, 1, 0, 196609, 1, 0, 196610, 1, 0 )

[node name="Label" type="Label" parent="Game/Control" index="1"]
visible = false
margin_top = 273.827
margin_bottom = 287.827
grow_horizontal = 2
Expand Down
Expand Up @@ -122,4 +122,4 @@ resource_local_to_scene = true

[node name="TileMap" parent="Game/Control" index="0"]
tile_set = SubResource( 1 )
tile_data = PoolIntArray( 131067, 1, 0, 131068, 1, 0, 131069, 1, 0, 131070, 1, 0, 131071, 1, 0, 65536, 1, 0, 65537, 1, 0, 65538, 1, 0, 65539, 1, 0, 65540, 1, 0, 196603, 1, 0, 196605, 3, 0, 131076, 1, 0, 262139, 1, 0, 262142, 6, 0, 262143, 7, 0, 196608, 7, 0, 196610, 2, 0, 196612, 1, 0, 327675, 1, 0, 327678, 6, 0, 327679, 2, 0, 262144, 3, 0, 262145, 0, 0, 262146, 7, 0, 262148, 1, 0, 393211, 1, 0, 393214, 4, 0, 393215, 7, 0, 327680, 7, 0, 327682, 4, 0, 327683, 5, 0, 327684, 1, 0, 458747, 1, 0, 458748, 3, 0, 393220, 1, 0, 524283, 1, 0, 524284, 1, 0, 524285, 1, 0, 524286, 1, 0, 524287, 1, 0, 458752, 1, 0, 458753, 1, 0, 458754, 1, 0, 458755, 1, 0, 458756, 1, 0 )
tile_data = PoolIntArray( 131069, 1, 0, 131070, 1, 0, 131071, 1, 0, 65536, 1, 0, 65537, 1, 0, 65538, 1, 0, 196605, 1, 0, 196606, 0, 0, 196607, 6, 0, 131072, 0, 0, 131073, 0, 0, 131074, 1, 0, 262141, 1, 0, 262142, 3, 0, 262143, 7, 0, 196608, 0, 0, 196609, 5, 0, 196610, 1, 0, 327677, 1, 0, 327678, 0, 0, 327679, 6, 0, 262144, 7, 0, 262145, 0, 0, 262146, 1, 0, 393213, 1, 0, 393215, 0, 0, 327680, 5, 0, 327681, 2, 0, 327682, 1, 0, 458749, 1, 0, 458750, 7, 0, 458751, 0, 0, 393216, 2, 0, 393217, 4, 0, 393218, 1, 0, 524285, 1, 0, 524286, 1, 0, 524287, 1, 0, 458752, 1, 0, 458753, 1, 0, 458754, 1, 0 )

0 comments on commit 9315c4d

Please sign in to comment.