State Design pattern and infinite loops with MonoGame - c#

I am trying to use state design pattern with my game(MonoGame game).I want to detect collision , so I give cell type and upon it gives me a specific action.But unfortunately this implementation gives infinite loop "Exception.StackOverflow" in player instances.
abstract class PlayerState
{
abstract public void doAction(Player player );
}
Classes implements PlayerState
class DeadState:PlayerState
{
public override void doAction(Player player)
{
player.dead();
player.setState(this);
}
}
class IncreaseGold: PlayerState
{
public override void doAction(Player player)
{
player.increaseGold();
player.setState(this);
}
}
Player.cs
public void setState(PlayerState state) { this.state = state; }
public PlayerState getState() { return state; }
ActionController.cs
Player player = new Player();
public void passCell(int cellType)
{
if (cellType == 1)
{
new DeadState().doAction(player);
}
if (cellType == 2)
{
new IncreaseGold().doAction(player);
}
}
finally in my Update method on Game1.cs
protected override void Update(GameTime gameTime)
{
player.passCell(cellType);
base.Update(gameTime);
}

Related

Why does my Button not get the right Onclick event in Unity?

I have implemented the state pattern for my game in Unity.
Thus I have a main class (BattleSystem) which starts by calling different states.
One of those states is the AttackState. The AttackState has an attackButton which is an UI Button (using UnityEngine.UI).
My State class looks like this:
public abstract class State : MonoBehaviour
{
protected BattleSystem battleSystem;
public abstract void Tick();
public virtual void OnStateEnter() { }
public virtual void OnStateExit() { }
public State(BattleSystem battleSystem) {
this.battleSystem = battleSystem;
}
}
My StateMachine class looks like this:
public abstract class StateMachine : MonoBehaviour
{
protected State state;
public void SetState(State state) {
this.state = state;
state.OnStateEnter();
}
}
The Button is in the BattleSystem class like this:
public class BattleSystem : StateMachine
{
public Button attackButton;
// Start is called before the first frame update
void Start()
{
SetState(new AttackState(this));
}
I dragged the AttackButton object onto my attackButton field in the inspector in the Unity editor.
My AttackState.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AttackState : State
{
public AttackState(BattleSystem battleSystem) : base(battleSystem)
{
}
public override void Tick()
{
}
public override void OnStateEnter()
{
Debug.Log("AttackState");
battleSystem.attackButton.onClick.AddListener(delegate () { this.Attack(); });
Debug.Log(battleSystem.attackButton.gameObject.name);
}
public void Attack() {
Debug.Log("Attacking");
foreach (Transform child in battleSystem.cardDropZone.transform)
{
Card card = (Card)child.GetComponent("Card");
// inflict damage to opponent equal to attack of cards
if (battleSystem.canDamageBeInflicted(card))
{
battleSystem.inflictDamage(card);
// adjust crystalCount
TurnUtilities.crystalCount -= card.crystalCost;
// Move card to graveyard after it has been used
battleSystem.sendToGraveyard(child);
battleSystem.attackButton.GetComponentInChildren<Text>().text = "End Turn";
}
else
{
Debug.Log("not enough crystals");
}
}
if (PlayerIsDead())
{
battleSystem.isPlayerDead = true;
battleSystem.SetState(new GameEndingState(battleSystem));
}
if (EnemyIsDead())
{
battleSystem.isEnemyDead = true;
battleSystem.SetState(new GameEndingState(battleSystem));
}
}
bool PlayerIsDead()
{
if (battleSystem.playerHealthbar.value <= 0)
{
return true;
}
return false;
}
bool EnemyIsDead()
{
if (battleSystem.enemyHealthbar.value <= 0)
{
return true;
}
return false;
}
}
The funny thing is that the Debug.Log(battleSystem.attackButton.gameObject.name); works and gives me the name of the GameObject. The event is not being registered though. What did I miss?
Picture of my Scene:
EDIT: I found out that this code works:
btn.onClick.AddListener(() => Debug.Log("Test"));
Putting the method inside does not work? What is happening here :D

I'm having trouble calling a method from one class to another

I'm trying to call this method from my script enemy to my script player can anyone guide me?
The first namespace of code is enemy second is player
private void OnTriggerEnter2D(Collider2D other)
{
DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
ProcessHit(damageDealer);
}
public void ProcessHit(DamageDealer damageDealer)
{
health -= damageDealer.GetDamage();
if (health <= 0)
{
Destroy(gameObject);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
}
Try having an abstract base class that is common for both Enemy and Player- E.g Character or whatever makes sense for your program.
public abstract class Character
{
public virtual void OnTriggerEnter2D(Collider2D other)
{
DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
ProcessHit(damageDealer);
};
public void ProcessHit(DamageDealer damageDealer)
{
//ProcessHit accessible for all classes that inherit Character
health -= damageDealer.GetDamage();
if (health <= 0)
{
Destroy(gameObject);
}
}
}
public class Enemy : Character
{
//this has both methods as declared in Character
}
public class Player : Character
{
// this needs to be overriden for Player
public override void OnTriggerEnter2D(Collider2D other)
{
//Enter OnTriggerEnter2D logic for Player
}
}
So if you want any class that has OnTriggerEnter2D to act in a different way, just override it and change the logic.

Scripting in unity 3d

I am facing a problem in Unity3D. I have the same health script attached to both the player and the enemy. I want to show the game-over message when the player dies but the issue is that the game over message appears for both the player and enemy when they die.
My code looks like is that:
public class CharacterStats : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
health = Mathf.Clamp (health, 0, 100);
}
public void damage(float damage)
{
health -= damage;
if(health<=0)
{
Die();
Application.LoadLevel(gameover);
}
}
void Die()
{
characterController.enabled = false;
if (scriptstodisable.Length == 0)
return;
foreach (MonoBehaviour scripts in scriptstodisable)
scripts.enabled = false;
if (ragdollmanger != null)
ragdollmanger.Raggdoll();
}
}
As you are using 1 script for both player and enemy. You should have different classes for both and implement an interface or derive from a base class to implement health:
public class Character : MonoBehaviour
{
public float Health;
public virtual void Damage(float damageValue)
{
Health -= damageValue;
}
public virtual void Die()
{
}
}
public Enemy : Character
{
public override void Die()
{
// do enemy related stuff
}
}
public Player : Character
{
public override void Die()
{
// do player related stuff.
// like game over screen
}
}
Hope this helps :)
You could use a bool to check whether CharacterStats is attached to the player, for example by adding a tag called ”Player” to the player game object and checking if gameObject.tag == “Player”, or you could equivalently name the game object ”Player” and check gameObject.name if you so wish.
You could then run the function for the game over message only if the game object is a player (isPlayer is true).
public class CharacterStats : MonoBehaviour
{
bool isPlayer = false;
// Use this for initialization
void Start ()
{
if(gameObject.tag == “Player”)
{
isPlayer = true;
}
}
// Update is called once per frame
void Update ()
{
health = Mathf.Clamp (health, 0, 100);
}
public void damage(float damage)
{
health -= damage;
if(health<=0)
{
if(isPlayer)
{
// Do Player-only stuff
}
// Do Stuff for both players and enemies
}
}
}

C# XNA Add a GameComponent from within a GameComponent

public class Menu : DrawableGameComponent
{
ContentManager Content;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Audio MenuMusic;
public Menu(Game game) : base(game)
{
spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
graphics = Game.Services.GetService(typeof(GraphicsDeviceManager)) as GraphicsDeviceManager;
Content = game.Content;
Content.RootDirectory = #"Content\Menu\";
*MenuMusic = new Audio(game);* // Instantiate the new DrawableGameComponent
Game.Components.Add(this);
MenuMusic.PauseTune = false;
}
public override void Initialize()
{
Menustate = MenuState.LoadContent;
base.Initialize();
}
protected override void LoadContent()
{
base.LoadContent();
}
protected override void UnloadContent()
{
base.UnloadContent();
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
}
}
public class Audio : DrawableGameComponent
{
public bool PauseTune
{
get { return PauseTune; }
set { PauseTune = value; }
}
SoundEffect Tune = null;
SoundEffectInstance SFXInstance;
public Audio(Game game) : base(game)
{
*game.Components.Add(this)*;// This is the problem. It adds an entirely new Game object. :(
}
public override void Initialize()
{
PauseTune = true;
base.Initialize();
}
protected override void LoadContent()
{
switch (Game1.Gamestate)
{
case GameState.Menu:
string AudioPath = #"Audio\";
Tune = Game.Content.Load<SoundEffect>(AudioPath + "Tune");
break;
case GameState.InitialiseGame:
break;
case GameState.PlayGame:
break;
default:
break;
}
if (Tune != null) SFXInstance = Tune.CreateInstance();
base.LoadContent();
}
protected override void UnloadContent()
{
base.UnloadContent();
}
public override void Update(GameTime gameTime)
{
if (Tune != null)
{
if (PauseTune)
{
if (SFXInstance.State == SoundState.Playing)
SFXInstance.Pause();
}
else
{
if (SFXInstance.State != SoundState.Playing)
SFXInstance.Play();
}
}
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
}
}
The probelem comes when I use the Menu to add a new Audio class instance.
The Audio constructor attempts to add the new GameCompent but fails.
What it actually does is create an entirely new Game instance, which then goes on to instantiate a new Menu that attempts to add a new Audio class instance..... Until I eventually get a - would you beleive? - stackoverflow error.
What am I doing wrong? / How can I add one component from within another?
DOH! Some days I should never turn on my computer :)
Or better yet - after a long, tiring day do NOT mess with mycode.
A self-referencing PauseTune property.

C# XNA - Background music logic

So I'm working with the XNA game states sample. I have 2 different songs that play and loop in the game states. That means:
mainMenu.mp3 goes to the mainMenu game state
backgroundMusic.mp3 goes to the Playing game state
So this works. But I also have the Escape key logic, which returns you from any state to the mainMenu state (when Esc is pressed). The song from Playing stops and the song from mainMenu begins playing. But if I click the Start Game button which moves me to Playing the background music is not being played. Where could be the problem?
Here's my code:
// Main Menu game state
if (Game1.g_CurrentGameState == Game1.g_GameStates.g_MainMenu && !m_SongStart)
{
MediaPlayer.Play(m_MainMenuTheme);
m_SongStart = true; // bool
}
if (Game1.g_CurrentGameState != Game1.g_GameStates.g_MainMenu)
{
MediaPlayer.Stop();
m_SongStart = false; // bool
}
// Playing game state
if (Game1.g_CurrentGameState == Game1.g_GameStates.g_GamePlaying && !m_SongStart)
{
MediaPlayer.Play(m_GamePlayingMainTheme);
m_SongStart = true;
}
if (Game1.g_CurrentGameState != Game1.g_GameStates.g_GamePlaying)
{
MediaPlayer.Stop();
m_SongStart = false;
}
The following code will play a song according current game state:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace WindowsGame1
{
public class Game1 : Game
{
private GameState _gameState;
private GraphicsDeviceManager _graphicsDeviceManager;
private KeyboardState _keyboardState;
private Song _song1;
private Song _song2;
public Game1() {
_graphicsDeviceManager = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void LoadContent() {
_song1 = Content.Load<Song>("song1");
_song2 = Content.Load<Song>("song2");
}
protected override void UnloadContent() {
if (_song1 != null) _song1.Dispose();
if (_song2 != null) _song2.Dispose();
}
protected override void Update(GameTime gameTime) {
GameState gameState = _gameState;
var keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.A) && _keyboardState.IsKeyUp(Keys.A)) {
gameState = GameState.Menu;
}
else if (keyboardState.IsKeyDown(Keys.B) && _keyboardState.IsKeyUp(Keys.B)) {
gameState = GameState.InGame;
}
else if (keyboardState.IsKeyDown(Keys.C) && _keyboardState.IsKeyUp(Keys.C)) {
gameState = GameState.Undefined;
}
_keyboardState = keyboardState;
if (gameState != _gameState) {
switch (gameState) {
case GameState.Undefined:
MediaPlayer.Stop();
break;
case GameState.Menu:
MediaPlayer.Stop();
MediaPlayer.Play(_song1);
break;
case GameState.InGame:
MediaPlayer.Stop();
MediaPlayer.Play(_song2);
break;
default:
throw new ArgumentOutOfRangeException();
}
_gameState = gameState;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
internal enum GameState
{
Undefined,
Menu,
InGame
}
}
Why not just write a wrapper class to wrap both a song and game state member in it and play the song when the current game state is equal to that one? An oop version of #Aybe's answer. Credit to him!
Here is what I've come up with. Should be easy to use and manage. Pretty simple.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Collections.Generic;
namespace WindowsGame1
{
public class GameStateManager
{
private static List<GameState> GameStates = new List<GameState>();
private static GameState CurrentGameState = null;
public static void AddState(GameState gameState)
{
GameStates.Add(gameState);
}
public static GameState GetState(string name)
{
return GameStates.Find(gameState => gameState.Name == name);
}
public static void SwitchStates(GameState gameState)
{
if (CurrentGameState != null)
{
if (MediaPlayer.State == MediaState.Playing)
MediaPlayer.Stop();
}
CurrentGameState = gameState;
if (CurrentGameState != null)
MediaPlayer.Play(CurrentGameState.Song);
}
public static void SwitchStates(string gameState)
{
SwitchStates(GetState(gameState));
}
public static void Dispose()
{
foreach (var gameState in GameStates)
{
gameState.Song.Dispose();
}
GameStates.Clear();
}
}
public class GameState
{
public Song Song;
public string Name;
public GameState(string name, Song song)
{
Name = name;
Song = song;
}
}
public class Game1 : Game
{
private GraphicsDeviceManager _graphicsDeviceManager;
private KeyboardState _keyboardState;
public Game1()
{
_graphicsDeviceManager = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void LoadContent()
{
GameStateManager.AddState(new GameState("InGame", Content.Load<Song>("song1")));
GameStateManager.AddState(new GameState("Menu", Content.Load<Song>("song2")));
}
protected override void UnloadContent()
{
GameStateManager.Dispose();
}
protected override void Update(GameTime gameTime)
{
var keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.A) && _keyboardState.IsKeyUp(Keys.A))
{
GameStateManager.SwitchStates("Menu");
}
else if (keyboardState.IsKeyDown(Keys.B) && _keyboardState.IsKeyUp(Keys.B))
{
GameStateManager.SwitchStates("InGame");
}
else if (keyboardState.IsKeyDown(Keys.C) && _keyboardState.IsKeyUp(Keys.C))
{
GameStateManager.SwitchStates("Undefined");
}
_keyboardState = keyboardState;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}

Categories