Skip to content

Commit

Permalink
Started to switch to TileMapLayers
Browse files Browse the repository at this point in the history
  • Loading branch information
Killarexe committed Sep 6, 2024
1 parent 3d0ade5 commit 73b04d1
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 202 deletions.
7 changes: 6 additions & 1 deletion assets/Tileset.tres
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[gd_resource type="TileSet" load_steps=32 format=3 uid="uid://dcovkjpnowgl0"]
[gd_resource type="TileSet" load_steps=34 format=3 uid="uid://dcovkjpnowgl0"]

[ext_resource type="Texture2D" uid="uid://b0b34lhgbuxy5" path="res://assets/textures/tilesets/tileset.png" id="1_3ut7y"]
[ext_resource type="PackedScene" uid="uid://dfamu2vvdstal" path="res://scenes/bundles/levels/tiles/CheckpointTile.tscn" id="1_ax33w"]

[sub_resource type="TileSetScenesCollectionSource" id="TileSetScenesCollectionSource_u2aof"]
scenes/1/scene = ExtResource("1_ax33w")

[sub_resource type="OccluderPolygon2D" id="OccluderPolygon2D_wfcrf"]
polygon = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8)
Expand Down Expand Up @@ -574,3 +578,4 @@ terrain_set_0/terrain_4/color = Color(1, 1, 1, 1)
custom_data_layer_0/name = "type"
custom_data_layer_0/type = 2
sources/1 = SubResource("TileSetAtlasSource_ddag5")
sources/0 = SubResource("TileSetScenesCollectionSource_u2aof")
60 changes: 30 additions & 30 deletions assets/style/default_theme.tres

Large diffs are not rendered by default.

24 changes: 16 additions & 8 deletions scenes/bundles/DefaultLevel.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
[ext_resource type="TileSet" uid="uid://dcovkjpnowgl0" path="res://assets/Tileset.tres" id="1_ae557"]
[ext_resource type="Script" path="res://scripts/level/LevelPlayer.gd" id="2_ukgah"]

[node name="DefaultLevel" type="TileMap"]
tile_set = ExtResource("1_ae557")
format = 2
layer_0/name = "Ground"
layer_1/name = "Background"
layer_1/modulate = Color(0.501961, 0.501961, 0.501961, 1)
layer_1/z_index = -2
layer_1/tile_data = PackedInt32Array()
[node name="DefaultLevel" type="Node2D" node_paths=PackedStringArray("ground", "background")]
script = ExtResource("2_ukgah")
ground = NodePath("Ground")
background = NodePath("Background")
mode = null
start_pos = null

[node name="Ground" type="TileMapLayer" parent="."]
use_parent_material = true
tile_set = ExtResource("1_ae557")

[node name="Background" type="TileMapLayer" parent="."]
modulate = Color(0.501961, 0.501961, 0.501961, 1)
z_index = -2
use_parent_material = true
tile_set = ExtResource("1_ae557")
collision_enabled = false

This file was deleted.

41 changes: 41 additions & 0 deletions scenes/bundles/levels/tiles/CheckpointTile.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[gd_scene load_steps=8 format=3 uid="uid://dfamu2vvdstal"]

[ext_resource type="Script" path="res://scripts/level/tiles/CheckpointTile.gd" id="1_en437"]
[ext_resource type="Texture2D" uid="uid://b0b34lhgbuxy5" path="res://assets/textures/tilesets/tileset.png" id="2_3mybj"]
[ext_resource type="PackedScene" uid="uid://ci0p37ottv00" path="res://scenes/bundles/particles/DefaultCheckpointParticle.tscn" id="2_k1qwm"]

[sub_resource type="AtlasTexture" id="AtlasTexture_0a1jv"]
atlas = ExtResource("2_3mybj")
region = Rect2(32, 48, 16, 16)

[sub_resource type="AtlasTexture" id="AtlasTexture_bkckm"]
atlas = ExtResource("2_3mybj")
region = Rect2(64, 48, 16, 16)

[sub_resource type="RectangleShape2D" id="RectangleShape2D_os5gs"]
size = Vector2(16, 16)

[sub_resource type="RectangleShape2D" id="RectangleShape2D_epn34"]
size = Vector2(18, 18)

[node name="CheckpointTile" type="StaticBody2D" node_paths=PackedStringArray("tile_off_texture", "interatcion_area")]
collision_mask = 8
script = ExtResource("1_en437")
tile_off_texture = NodePath("TileOff")
checkpoint_particle = ExtResource("2_k1qwm")
interatcion_area = NodePath("InteractionArea")

[node name="TileOn" type="Sprite2D" parent="."]
texture = SubResource("AtlasTexture_0a1jv")

[node name="TileOff" type="Sprite2D" parent="."]
texture = SubResource("AtlasTexture_bkckm")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_os5gs")

[node name="InteractionArea" type="Area2D" parent="."]
collision_mask = 8

[node name="CollisionShape2D" type="CollisionShape2D" parent="InteractionArea"]
shape = SubResource("RectangleShape2D_epn34")
26 changes: 15 additions & 11 deletions scenes/levels/Level2.tscn
100755 → 100644

Large diffs are not rendered by default.

24 changes: 13 additions & 11 deletions scenes/levels/TitleScreenLevel.tscn

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions scenes/uis/MainMenu.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ libraries = {
}

[node name="TitleScreenLevel" parent="." instance=ExtResource("3_gaxhl")]
start_pos = null

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

Expand Down
8 changes: 6 additions & 2 deletions scenes/uis/SoloMenu.tscn

Large diffs are not rendered by default.

15 changes: 0 additions & 15 deletions scripts/level/Level.gd
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
extends ResourceElement
class_name Level

enum Mode{
RACE, #Classic singleplayer
BATTLE #Classic multiplayer
}

enum LevelTheme{
PLAINS,
CAVES,
Expand All @@ -25,27 +20,17 @@ enum Difficulty{

const DEFAULT_NAME: String = "MyLevel"
const DEFAULT_Y_LIMIT: int = 512
const DEFAULT_MODE: Mode = Mode.RACE
const DEFAULT_DESCRIPTION: String = "Hello, world!"
const DEFAULT_DIFFICULTY: Difficulty = Difficulty.NORMAL

@export var name: String = ""
@export var is_hidden: bool = false
@export_multiline var description: String = ""
@export var mode: Mode = DEFAULT_MODE
@export_range(128, 1024) var y_limit: int = DEFAULT_Y_LIMIT
@export var scene: PackedScene = PackedScene.new()
@export var level_theme: LevelTheme = LevelTheme.PLAINS
@export var difficulty: Difficulty = DEFAULT_DIFFICULTY

static func mode_to_str(level_mode: Mode) -> String:
match level_mode:
Mode.RACE:
return "level.mode.race"
Mode.BATTLE:
return "level.mode.battle"
return "unknown"

static func difficulty_to_str(level_diffuculty: Difficulty) -> String:
match level_diffuculty:
Difficulty.BEGINER_FRIENDLY:
Expand Down
110 changes: 13 additions & 97 deletions scripts/level/LevelPlayer.gd
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,60 +1,28 @@
extends TileMap
extends Node
class_name LevelPlayer

const ON_TILE: Vector2i = Vector2i(0, 5)
const OFF_TILE: Vector2i = Vector2i(0, 6)

const RED_FULL_TILE: Vector2i = Vector2i(1, 5)
const RED_EMPTY_TILE: Vector2i = Vector2i(1, 6)

const BLUE_FULL_TILE: Vector2i = Vector2i(1, 7)
const BLUE_EMPTY_TILE: Vector2i = Vector2i(1, 8)

const CHECKPOINT_ON_TILE: Vector2i = Vector2i(2, 3)
const CHECKPOINT_OFF_TILE: Vector2i = Vector2i(4, 3)

const BLUE_SPIKE_TILE_DOWN_ON: Vector2i = Vector2i(2, 9)
const BLUE_SPIKE_TILE_DOWN_OFF: Vector2i = Vector2i(4, 9)
const RED_SPIKE_TILE_DOWN_ON: Vector2i = Vector2i(6, 9)
const RED_SPIKE_TILE_DOWN_OFF: Vector2i = Vector2i(8, 9)

const BLUE_SPIKE_TILE_UP_ON: Vector2i = Vector2i(2, 8)
const BLUE_SPIKE_TILE_UP_OFF: Vector2i = Vector2i(4, 8)
const RED_SPIKE_TILE_UP_ON: Vector2i = Vector2i(6, 8)
const RED_SPIKE_TILE_UP_OFF: Vector2i = Vector2i(8, 8)

const BLUE_SPIKE_TILE_LEFT_ON: Vector2i = Vector2i(3, 8)
const BLUE_SPIKE_TILE_LEFT_OFF: Vector2i = Vector2i(5, 8)
const RED_SPIKE_TILE_LEFT_ON: Vector2i = Vector2i(7, 8)
const RED_SPIKE_TILE_LEFT_OFF: Vector2i = Vector2i(9, 8)

const BLUE_SPIKE_TILE_RIGHT_ON: Vector2i = Vector2i(3, 9)
const BLUE_SPIKE_TILE_RIGHT_OFF: Vector2i = Vector2i(5, 9)
const RED_SPIKE_TILE_RIGHT_ON: Vector2i = Vector2i(7, 9)
const RED_SPIKE_TILE_RIGHT_OFF: Vector2i = Vector2i(9, 9)

enum Mode{
enum Mode {
PLAY,
EDIT
}

const CHANGE_COLOR_TIME: float = 0.5

@export_category("Level Dependencies")
@export var ground: TileMapLayer
@export var background: TileMapLayer
@export_category("Level Settings")
@export var mode: Mode = Mode.PLAY
@export var start_pos: Vector2 = Vector2()
var player_prefab: Resource = load("res://scenes/bundles/player/Player.tscn")
@export var start_pos: Vector2 = Vector2.ZERO

var change_color_time: float = 0.0
var player_prefab: Resource = load("res://scenes/bundles/player/Player.tscn")

func _ready():
if Game.current_event == Game.Event.CHRISTMAS:
tile_set.get_source(1).texture = load("res://assets/textures/tilesets/plains_christmas.png")
var texture = load("res://assets/textures/tilesets/plains_christmas.png")
ground.tile_set.get_source(1).texture = texture
background.tile_set.get_source(1).texture = texture
if mode == Mode.PLAY:
spawn_player()

func _process(delta: float):
change_color_time -= delta

func finish_level(player: PlayerComponent, death: DeathComponent, time: PlayerTimer):
var parent: Node = get_parent()
if parent is LevelEditor:
Expand All @@ -64,67 +32,15 @@ func finish_level(player: PlayerComponent, death: DeathComponent, time: PlayerTi

func spawn_player():
var player = Game.instanceNodeAtPos(player_prefab, self, start_pos)
player.connect("on_switch_color", switch_colors)
player.connect("on_setting_spawnpoint", set_spawnpoint)

func filter_used_grass_cells() -> Array[Vector2i]:
var used_grass_cells: Array[Vector2i] = [];
for cell in get_used_cells(0):
var type: Vector2i = get_cell_atlas_coords(0, cell)
for cell in ground.get_used_cells():
var type: Vector2i = ground.get_cell_atlas_coords(cell)
if type.x >= 0 && type.x <= 9 && type.y >= 0 && type.y <= 2:
used_grass_cells.append(cell)
return used_grass_cells

#Color: true = red, false = blue
func switch_colors(color: bool):
if change_color_time > 0.0:
return
change_color_time = CHANGE_COLOR_TIME
if color:
replace_tile_by(ON_TILE, OFF_TILE)

replace_tile_by(RED_FULL_TILE, RED_EMPTY_TILE)
replace_tile_by(RED_SPIKE_TILE_DOWN_ON, RED_SPIKE_TILE_DOWN_OFF)
replace_tile_by(RED_SPIKE_TILE_UP_ON, RED_SPIKE_TILE_UP_OFF)
replace_tile_by(RED_SPIKE_TILE_LEFT_ON, RED_SPIKE_TILE_LEFT_OFF)
replace_tile_by(RED_SPIKE_TILE_RIGHT_ON, RED_SPIKE_TILE_RIGHT_OFF)

replace_tile_by(BLUE_EMPTY_TILE, BLUE_FULL_TILE)
replace_tile_by(BLUE_SPIKE_TILE_DOWN_OFF, BLUE_SPIKE_TILE_DOWN_ON)
replace_tile_by(BLUE_SPIKE_TILE_UP_OFF, BLUE_SPIKE_TILE_UP_ON)
replace_tile_by(BLUE_SPIKE_TILE_LEFT_OFF, BLUE_SPIKE_TILE_LEFT_ON)
replace_tile_by(BLUE_SPIKE_TILE_RIGHT_OFF, BLUE_SPIKE_TILE_RIGHT_ON)
else:
replace_tile_by(OFF_TILE, ON_TILE)

replace_tile_by(BLUE_FULL_TILE, BLUE_EMPTY_TILE)
replace_tile_by(BLUE_SPIKE_TILE_DOWN_ON, BLUE_SPIKE_TILE_DOWN_OFF)
replace_tile_by(BLUE_SPIKE_TILE_UP_ON, BLUE_SPIKE_TILE_UP_OFF)
replace_tile_by(BLUE_SPIKE_TILE_LEFT_ON, BLUE_SPIKE_TILE_LEFT_OFF)
replace_tile_by(BLUE_SPIKE_TILE_RIGHT_ON, BLUE_SPIKE_TILE_RIGHT_OFF)

replace_tile_by(RED_EMPTY_TILE, RED_FULL_TILE)
replace_tile_by(RED_SPIKE_TILE_DOWN_OFF, RED_SPIKE_TILE_DOWN_ON)
replace_tile_by(RED_SPIKE_TILE_UP_OFF, RED_SPIKE_TILE_UP_ON)
replace_tile_by(RED_SPIKE_TILE_LEFT_OFF, RED_SPIKE_TILE_LEFT_ON)
replace_tile_by(RED_SPIKE_TILE_RIGHT_OFF, RED_SPIKE_TILE_RIGHT_ON)

func set_spawnpoint(cell_pos: Vector2):
replace_tile_by(CHECKPOINT_ON_TILE, CHECKPOINT_OFF_TILE)
set_cell(0, cell_pos, 1, CHECKPOINT_ON_TILE, 0)

func replace_tile_by(original_tile_id: Vector2i, new_tile_id: Vector2i):
for cell in get_used_cells_by_id(0, 1, original_tile_id, 0):
set_cell(0, Vector2(cell.x, cell.y), 1, new_tile_id, 0)

func change_tile_and_update(tile_pos: Vector2i, tile_id: Vector2i):
set_cell(0, tile_pos, 1, tile_id, 0)
set_cells_terrain_connect(0, filter_used_grass_cells(), 0, 0, false)

func remove_tile_and_update(tile_pos: Vector2i):
set_cell(0, tile_pos, 1, Vector2i(-1, -1), 0)
set_cells_terrain_connect(0, filter_used_grass_cells(), 0, 0, false)

func set_mode(new_mode: Mode):
mode = new_mode
if mode == Mode.PLAY:
Expand Down
13 changes: 13 additions & 0 deletions scripts/level/tiles/CheckpointTile.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extends InteractiveTile
class_name CheckpointTile

@export_category("Checkpoint components")
@export var tile_off_texture: Sprite2D
@export var checkpoint_particle: PackedScene

func on_body_entered(body: Node2D) -> void:
if body is PlayerComponent && tile_off_texture.visible:
tile_off_texture.visible = false
var spawn_point: Vector2 = global_position - Vector2(0, 32)
body.get_node("DeathComponent").spawn_point = spawn_point
Game.instanceNodeAtPos(checkpoint_particle, body.get_parent(), spawn_point)
15 changes: 15 additions & 0 deletions scripts/level/tiles/InteractiveTile.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extends StaticBody2D
class_name InteractiveTile

@export_category("Interaction Tile Dependencies")
@export var interatcion_area: Area2D

func _ready() -> void:
interatcion_area.body_entered.connect(on_body_entered)
interatcion_area.body_exited.connect(on_body_exited)

func on_body_entered(body: Node2D) -> void:
pass

func on_body_exited(body: Node2D) -> void:
pass
1 change: 0 additions & 1 deletion scripts/uis/level_editor/LevelEditor.gd
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func _on_save_button_pressed():
level.name = level_name
level.is_hidden = false
level.scene = packed_scene
level.mode = Level.Mode.RACE
level.y_limit = level_settings.y_limit
level.level_theme = Level.LevelTheme.PLAINS
level.difficulty = level_settings.difficulty
Expand Down

0 comments on commit 73b04d1

Please sign in to comment.