Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created September 17, 2022 13:39
Show Gist options
  • Save kurtdekker/241c5ad3d3c131f57ebe53505a8f5c10 to your computer and use it in GitHub Desktop.
Save kurtdekker/241c5ad3d3c131f57ebe53505a8f5c10 to your computer and use it in GitHub Desktop.
Examples of ultra-simple seasonal behavior differences
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// @kurtdekker - this is part of Jetpack Kurt
//
// Spawns different items during certain seasons, in this case Christmas.
//
// To use:
// - place on empty active GameObject in your prefab or scene
// - move GameObject where you want seasonal item to spawn
// - supply alternate things to spawn (Christmas trees)
// - (optionally) supply original things to destroy if we make a Christmas tree
//
namespace Seasonal
{
public class SpawnOnChristmas : MonoBehaviour
{
// These dates could trivially be generalized for all seasons, but
// I found it was better on the workflow to have different versions
// for each seasonal event. YMMV.
// from C# docs: The month component, expressed as a value between 1 and 12.
const int FirstMonth = 12;
const int LastMonth = 12;
// from C# docs: The day component, expressed as a value between 1 and 31.
const int FirstDay = 10;
const int LastDay = 25;
[Header( "What might we spawn on Christmas?")]
public GameObject[] SpawnChoices;
[Header( "Chance from 0.0 to 1.0 of doing it.")]
public float SpawnChance;
[Header( "If we do spawn, should we destroy something else to make room?")]
public GameObject[] InstancesToDestroyWhenWeReplace;
void Reset()
{
SpawnChance = 1.0f;
}
// conveniently other code can reach in here and change
// other programmatic things like perhaps colors or music!
public static bool IsChristmas()
{
System.DateTime now = System.DateTime.Now;
if (now.Month >= FirstMonth && now.Month <= LastMonth)
{
if (now.Day >= FirstDay && now.Day <= LastDay)
{
return true;
}
}
return false;
}
void Start ()
{
bool spawn = IsChristmas();
if (Random.value > SpawnChance)
{
spawn = false;
}
if (spawn)
{
if (InstancesToDestroyWhenWeReplace != null)
{
foreach( var go in InstancesToDestroyWhenWeReplace)
{
Destroy(go);
}
}
int which = Random.Range( 0, SpawnChoices.Length);
Instantiate<GameObject>( SpawnChoices[which], transform);
}
}
void OnDrawGizmos()
{
// programmer art Christmas tree... your tree might
// be different size or shape, so hack up the numbers!
float height = 4.0f;
float skirtRadius = 1.0f;
float trunkRadius = 0.2f;
Gizmos.color = Color.red;
Vector3 pos0 = transform.position;
Vector3 pos1 = pos0 + Vector3.up * skirtRadius;
Vector3 pos2 = pos0 + Vector3.up * height;
Gizmos.DrawLine( pos1, pos2);
Gizmos.DrawWireCube( pos0, new Vector3( trunkRadius, skirtRadius * 2, trunkRadius));
Vector3 pos3prev = Vector3.zero;
for (int i = 0; i <= 4; i++)
{
var rot = Quaternion.Euler( 0, 90 * i, 0);
var pos3 = pos1 + rot * Vector3.right * skirtRadius;
if (i > 0)
{
Gizmos.DrawLine( pos3, pos3prev);
Gizmos.DrawLine( pos3, pos2);
}
pos3prev = pos3;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment