Skip to content

Commit

Permalink
💨 Make player enter and exit rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
tomrijnbeek committed Oct 1, 2023
1 parent a45c5bf commit eeef79b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
8 changes: 1 addition & 7 deletions entities/item/Encounter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Godot;

public sealed class Encounter
public sealed class Encounter
{
private readonly Player player;
private readonly Hazard hazard;
Expand All @@ -14,24 +12,20 @@ public Encounter(Player player, Hazard hazard)
public void DamageHazard(int amount)
{
hazard.TakeDamage(amount);
GD.Print($"Ka-pow! {hazard.Type} reduced to {hazard.CurrentHealth} health");
}

public void HealHazard(int amount)
{
hazard.Heal(amount);
GD.Print($"Uh-oh. {hazard.Type} increased to {hazard.CurrentHealth} health");
}

public void DamagePlayer(int amount)
{
player.TakeDamage(amount);
GD.Print($"Ouch! Player reduced to {player.CurrentHealth} health");
}

public void HealPlayer(int amount)
{
player.Heal(amount);
GD.Print($"Sparkles :3 Player increased to {player.CurrentHealth} health");
}
}
58 changes: 54 additions & 4 deletions scenes/room/Room.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
using System;
using System.Collections.Generic;
using Godot;

public sealed class Room : Node2D
{
private const float initialY = -16;
private const float restY = 32;
private const float finalY = 124;
private const float movementSpeed = 96;

[Signal]
public delegate void RoomExited();

private readonly Queue<IRoomStep> queuedSteps = new();
private Templates templates = null!;
private State state = State.Done;

// find a better way of injecting this?
public Player Player { get; set; } = null!;
Expand All @@ -17,34 +24,77 @@ public override void _Ready()
templates = GetNode<Templates>("/root/Templates");
}

public override void _Process(float delta)
{
if (state is State.Steps or State.Done)
{
return;
}

Player.Position = new Vector2(Player.Position.x, Player.Position.y + delta * movementSpeed);
if (state == State.Entering && Player.Position.y >= restY)
{
Player.Position = new Vector2(Player.Position.x, restY);
state = State.Steps;
doNextStep();
}

if (state == State.Exiting && Player.Position.y >= finalY)
{
finishRoom();
}
}

public void FillRoom(RoomContents contents)
{
if (state != State.Done)
{
throw new InvalidOperationException();
}

queuedSteps.Clear();
foreach (var step in contents.Steps)
{
queuedSteps.Enqueue(step);
}

// TODO: animate in
Player.Position = new Vector2(96, 24);
enterRoom();
}

doNextStep();
private void enterRoom()
{
Player.Position = new Vector2(96, initialY);
state = State.Entering;
}

private void doNextStep()
{
if (queuedSteps.Count == 0)
{
finishRoom();
exitRoom();
return;
}

var step = queuedSteps.Dequeue();
step.Do(this, Player, templates, doNextStep);
}

private void exitRoom()
{
state = State.Exiting;
}

private void finishRoom()
{
state = State.Done;
EmitSignal(nameof(RoomExited));
}

private enum State
{
Entering,
Steps,
Exiting,
Done
}
}
2 changes: 0 additions & 2 deletions scenes/room/steps/EncounterStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public void Do(Node roomNode, Player player, Templates templates, Action complet

cursor.Connect(nameof(ItemSelect.ItemChosen), runner, nameof(runner.OnItemChosen));

GD.Print($"Beware! A {hazard}");

roomNode.AddChild(runner);
runner.AddChild(hazardObj);
runner.AddChild(cursor);
Expand Down
2 changes: 1 addition & 1 deletion scenes/story/Dialogue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override void _Process(float delta)
text.PercentVisible = Math.Min(text.PercentVisible + 0.5f * delta, 1.0f);
}

public override void _UnhandledInput(InputEvent @event)
public override void _Input(InputEvent @event)
{
if (!Visible)
{
Expand Down

0 comments on commit eeef79b

Please sign in to comment.