Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Last active July 8, 2021 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtdekker/d35da6b38a72a00ca118b0275a48a613 to your computer and use it in GitHub Desktop.
Save kurtdekker/d35da6b38a72a00ca118b0275a48a613 to your computer and use it in GitHub Desktop.
Same as AttackSequenceDemo but with coroutines
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// @kurtdekker
//
// from Unity forums: https://forum.unity.com/threads/logic-to-create-turns-of-attacks.1137946/
//
// Rewriting AttackSequencingDemo as coroutines, as per GroZZler's suggestion.
//
// Observation: process can be shorter in terms of LoC, but is the
// logic more-evident to a new user? Indeterminate...
//
// goal: a random attack starts in 2-5 seconds, you have 1 second to defend against it
//
// implementation: coroutines and timers
//
// To use:
// - make an empty scene
// - add a single GameObject
// - put this script on it
// - press PLAY
// - pay attention to the console
//
public class AttackSequencingDemo2 : MonoBehaviour
{
// goes up if you defend
int score;
// needed to win
const int ScoreRequired = 5;
// goes down if you fail to defend, game over at 0 hp
int lifeHitpoints = 3;
// it will choose one of these keys to defend
KeyCode[] possibleDefenseKeys = new KeyCode[]
{
KeyCode.A, KeyCode.S, KeyCode.D,
};
string GameStateDetails()
{
return "Score: " + score + ", HP: " + lifeHitpoints;
}
// Start is our master coroutine
IEnumerator Start()
{
while(true)
{
Debug.Log( "Get ready for a random attack... incoming...");
float randomCooldownTimer = Random.Range( 2.0f, 5.0f);
// wait for it to begin...
while( randomCooldownTimer > 0)
{
randomCooldownTimer -= Time.deltaTime;
CheckForSpammedKeys();
yield return null;
}
// choose how to defend
KeyCode chosenDefenseKey = possibleDefenseKeys[ Random.Range( 0, possibleDefenseKeys.Length)];
Debug.Log( "ATTACK BEGINS! Attack pending in 1 second! Press " + chosenDefenseKey + " to defend!!");
float attackIsUnderwayCountdown = 1.0f;
// wait for attack to land
while( true)
{
// check for keys, both legit and otherwise
if (Input.GetKeyDown( chosenDefenseKey))
{
score++;
Debug.Log( "DEFENDED! " + GameStateDetails());
break;
}
else
{
CheckForSpammedKeys();
}
attackIsUnderwayCountdown -= Time.deltaTime;
if (attackIsUnderwayCountdown < 0)
{
lifeHitpoints--;
Debug.Log( "YOU GOT HIT!! " + GameStateDetails());
break;
}
yield return null;
}
// necessary otherwise your last keystroke will be counted as spam at the top...
yield return null;
}
}
void CheckForSpammedKeys()
{
foreach( var key in possibleDefenseKeys)
{
if (Input.GetKeyDown( key))
{
score--;
if (score < 0)
{
score = 0;
}
Debug.Log( "PENALTY FOR SPAMMING! " + GameStateDetails());
// NOTE: even if you spam, the attack continues!
}
}
}
void UpdateHandleWinLose()
{
if (lifeHitpoints <= 0)
{
Debug.Log( "YOU DIED!");
Destroy(this);
return;
}
if (score >= ScoreRequired)
{
Debug.Log( "YOU WIN!");
Destroy(this);
return;
}
}
void Update()
{
UpdateHandleWinLose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment