Fixed names for a better traceability of code errors with orignal code lesson. End of lesson 25. Player is moving on the map viewport!
This commit is contained in:
47
scripts/game/maps/Map.gd
Normal file
47
scripts/game/maps/Map.gd
Normal file
@@ -0,0 +1,47 @@
|
||||
extends TileMap
|
||||
|
||||
class_name Map
|
||||
|
||||
enum Direction { North, East, South, West }
|
||||
|
||||
@export var defaultPlayerStartPosition:Vector2i
|
||||
@export var stepIncrement:float = 1.0
|
||||
|
||||
var player
|
||||
|
||||
func _ready():
|
||||
CommandDispatcher.PLAYER_MOVE.connect(onPlayerMove)
|
||||
|
||||
|
||||
func spawnPlayerAtPosition(position, facing):
|
||||
var spawnposition:Vector2i
|
||||
|
||||
if (position == null):
|
||||
spawnposition = defaultPlayerStartPosition
|
||||
else:
|
||||
spawnposition = position
|
||||
|
||||
player.position = Vector2(spawnposition.x * 16, spawnposition.y * 16)
|
||||
|
||||
|
||||
func onPlayerMove(direction):
|
||||
var newPos = Vector2(player.position)
|
||||
var moveIncrement = tile_set.tile_size.x * stepIncrement
|
||||
|
||||
match(direction):
|
||||
Direction.North:
|
||||
newPos.y -= moveIncrement
|
||||
Direction.South:
|
||||
newPos.y += moveIncrement
|
||||
Direction.West:
|
||||
newPos.x -= moveIncrement
|
||||
Direction.East:
|
||||
newPos.x += moveIncrement
|
||||
|
||||
if (playerCanMoveTo(newPos)):
|
||||
player.position = newPos
|
||||
player.updateAnimation(direction)
|
||||
|
||||
|
||||
func playerCanMoveTo(position) -> bool:
|
||||
return false
|
||||
@@ -12,9 +12,10 @@ func getTerrainDataForTile(layer, data, x, y):
|
||||
else:
|
||||
return null
|
||||
|
||||
|
||||
func spawnPlayerAtPosition(position, facing):
|
||||
player = load("res://scenes/game/maps/entities/player.tscn").instantiate()
|
||||
|
||||
|
||||
super.spawnPlayerAtPosition(position, facing)
|
||||
|
||||
return player
|
||||
@@ -8,16 +8,39 @@ func _ready():
|
||||
CommandDispatcher.LOAD_MAP.connect(loadMap)
|
||||
|
||||
|
||||
func loadMap(currentMapPath, newMapPath, spawnpoint, facing):
|
||||
var newMap:Map = load(newMapPath).instantiate()
|
||||
func loadMap(newMapPath, spawnpoint, facing):
|
||||
var newMap:Map
|
||||
|
||||
CommandDispatcher.PAUSE_PROCESSOR.emit()
|
||||
|
||||
if (currentMapPath != null):
|
||||
currentMapPath.queue_free()
|
||||
if (map != null):
|
||||
map.queue_free()
|
||||
|
||||
map = load(newMapPath).instantiate()
|
||||
|
||||
add_child(map)
|
||||
|
||||
entities.add_child(map.spawnPlayerAtPosition(spawnpoint, facing))
|
||||
|
||||
GameManager.currentMap = newMapPath
|
||||
|
||||
add_child(newMap)
|
||||
|
||||
entities.add_child(newMap.spawnPlayerAtPosition(spawnpoint, facing))
|
||||
|
||||
CommandDispatcher.WAIT_FOR_COMMAND.emit()
|
||||
|
||||
|
||||
func _unhandled_key_input(event):
|
||||
var direction
|
||||
|
||||
if (Input.is_action_pressed("ui_right")):
|
||||
direction = Map.Direction.East
|
||||
|
||||
if (Input.is_action_pressed("ui_left")):
|
||||
direction = Map.Direction.West
|
||||
|
||||
if (Input.is_action_pressed("ui_up")):
|
||||
direction = Map.Direction.North
|
||||
|
||||
if (Input.is_action_pressed("ui_down")):
|
||||
direction = Map.Direction.South
|
||||
|
||||
if (direction != null):
|
||||
CommandDispatcher.PROCESS_COMMAND.emit(MoveCommand.new(direction))
|
||||
|
||||
20
scripts/game/maps/WorldMap.gd
Normal file
20
scripts/game/maps/WorldMap.gd
Normal file
@@ -0,0 +1,20 @@
|
||||
extends Map2d
|
||||
|
||||
class_name WorldMap
|
||||
|
||||
func playerCanMoveTo(position:Vector2) -> bool:
|
||||
var tilesize = tile_set.tile_size.x
|
||||
var cast_cell_1
|
||||
var cast_cell_2
|
||||
|
||||
player.updateRaycasts(position)
|
||||
|
||||
cast_cell_1 = local_to_map(position + player.collisionRay_1.position)
|
||||
cast_cell_2 = local_to_map(position + player.collisionRay_2.position)
|
||||
|
||||
if (getTerrainDataForTile(0, TerrainDataTypes.TerrainType, cast_cell_1.x, cast_cell_1.y) != "Water" and
|
||||
getTerrainDataForTile(0, TerrainDataTypes.TerrainType, cast_cell_2.x, cast_cell_2.y) != "Water"):
|
||||
return true
|
||||
else:
|
||||
CommandDispatcher.DISPLAY_MESSAGE.emit("Your path is blocked.")
|
||||
return false
|
||||
@@ -1,19 +0,0 @@
|
||||
extends TileMap
|
||||
|
||||
class_name Map
|
||||
|
||||
enum Direction { North, East, South, West }
|
||||
|
||||
@export var defaultPlayerStartPosition:Vector2i
|
||||
|
||||
var player
|
||||
|
||||
func spawnPlayerAtPosition(position, facing):
|
||||
var spawnposition
|
||||
|
||||
if (position == null):
|
||||
spawnposition = defaultPlayerStartPosition
|
||||
else:
|
||||
spawnposition = position
|
||||
|
||||
player.position = Vector2(spawnposition.x * 16, spawnposition.y * 16)
|
||||
@@ -1,4 +0,0 @@
|
||||
extends Map2d
|
||||
|
||||
#func _ready() -> void:
|
||||
# super._ready()
|
||||
Reference in New Issue
Block a user