Skip to content

Instantly share code, notes, and snippets.

@morphingcoffee
Created January 16, 2022 18:02
Show Gist options
  • Save morphingcoffee/141eed6656a6b5cd54b252ae2c293bc3 to your computer and use it in GitHub Desktop.
Save morphingcoffee/141eed6656a6b5cd54b252ae2c293bc3 to your computer and use it in GitHub Desktop.
Unity C# Questing Example Snippets
using System;
using Newtonsoft.Json;
using UnityEngine;
[Serializable]
public class MeetQuest : Quest
{
[JsonProperty("meetPedestriansAmount")]
public int meetPedestriansAmount;
[JsonProperty("currentAmount")]
public int currentAmount;
public MeetQuest(string questName, string description, int meetPedestriansAmount)
{
// For superclass
this.questName = questName;
this.description = description;
// Quest-specific
this.meetPedestriansAmount = meetPedestriansAmount;
}
// Deserialization constructor
[JsonConstructor]
public MeetQuest() { }
override public void Start()
{
GameEventManager.Instance.OnMetPedestrianHandler += OnMeetPedestrian;
OnQuestStarted();
}
private void Complete()
{
GameEventManager.Instance.OnMetPedestrianHandler -= OnMeetPedestrian;
OnQuestEnded();
}
private void OnMeetPedestrian(Pedestrian pedestrian)
{
currentAmount += 1;
Debug.Log(DescriptionString() + ": met " + pedestrian.GetDialogue().name);
Debug.Log(StatusString());
if (currentAmount >= meetPedestriansAmount)
{
Complete();
}
}
private string StatusString()
{
return "Quest '" + questName + "' progress " + currentAmount + "/" + meetPedestriansAmount;
}
}
using System;
using Newtonsoft.Json;
/**
* Base quest class for more specific quest types
* to extend.
* Provides basic data fields & implementation for
* broadcasting quest completion state.
**/
public abstract class Quest
{
public event Action<Quest> OnQuestStartedHandler;
public event Action<Quest> OnQuestEndedHandler;
/**
* [JsonProperty] annotations allow serializer/deserializer to load properties
* with provided naming consistently, so that variable name refactoring could
* be done without breaking anything.
**/
[JsonProperty("questName")]
public string questName;
[JsonProperty("description")]
public string description;
[JsonProperty("completed")]
public bool completed;
[JsonProperty("rewardClaimed")]
public bool rewardClaimed;
[JsonProperty("rewards")]
public RewardData[] rewards;
/**
* Call this method to start tracking its progress
**/
public abstract void Start();
/**
* To be called by sub-classes when quest gets activated
* (starts tracking its progress)
**/
protected void OnQuestStarted()
{
UnityEngine.Debug.Log(DescriptionString() + " started.");
OnQuestStartedHandler?.Invoke(this);
}
/**
* To be called by sub-classes when quest gets de-activated
* (stops tracking its progress due to completion or failure)
**/
protected void OnQuestEnded()
{
UnityEngine.Debug.Log(DescriptionString() + " ended.");
completed = true;
OnQuestEndedHandler?.Invoke(this);
}
public string DescriptionString()
{
return "Quest '" + questName + "' (" + description + ")";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment