Skip to content
bozar42 edited this page Apr 13, 2020 · 3 revisions

06: Restrict Movement

Source code.

Detecting and handling collision is the most complicated part in the demo. Thus we divide it into two chapters. In this chapter, we restrict PC's movement inside the dungeon and outside walls. An invalid movement does not end PC's current turn. In Chapter 7, PC will be able to bump attack NPCs. PC's turn ends after attacking. We also need to remove dead NPCs.

Forbid PC To Leave The Dungeon

Check out commit: 54dc879.

First, comment print() lines in PCMove.gd and Schedule.gd so as not to fill the output board with unwanted text. Add DungeonBoard (Node2D node) to MainScene and attach DungeonBoard.gd to it.

# DungeonBoard.gd

func is_inside_dungeon(x: int, y: int) -> bool:
    return (x > -1) and (x < _new_DungeonSize.MAX_X) \
            and (y > -1) and (y < _new_DungeonSize.MAX_Y)

Let PCMove._try_move() call DungeonBoard.is_inside_dungeon() to forbid invalid movement.

# PCMove.gd

func _unhandled_input(event: InputEvent) -> void:
    var source: Array = _new_ConvertCoord.vector_to_array(_pc.position)
    var target: Array

    if _is_move_input(event):
        target = _get_new_position(event, source)
        _try_move(target[0], target[1])


func _try_move(x: int, y: int) -> void:
    if not _ref_DungeonBoard.is_inside_dungeon(x, y):
        print("Cannot leave dungeon.")
    else:
        set_process_unhandled_input(false)
        _pc.position = _new_ConvertCoord.index_to_vector(x, y)
        _ref_Schedule.end_turn()

Clone this wiki locally