Skip to content

Commit

Permalink
Add singleton GameManager class
Browse files Browse the repository at this point in the history
GameManager class allows me to move state code (death, health counter) out of player and into a more appropriate place.

It also allows for future functionality, like easy fade in of the death panel.
  • Loading branch information
mdschuster committed Sep 12, 2019
1 parent 5a63036 commit 50d617c
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 3 deletions.
56 changes: 56 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"files.exclude":
{
"**/.DS_Store":true,
"**/.git":true,
"**/.gitignore":true,
"**/.gitmodules":true,
"**/*.booproj":true,
"**/*.pidb":true,
"**/*.suo":true,
"**/*.user":true,
"**/*.userprefs":true,
"**/*.unityproj":true,
"**/*.dll":true,
"**/*.exe":true,
"**/*.pdf":true,
"**/*.mid":true,
"**/*.midi":true,
"**/*.wav":true,
"**/*.gif":true,
"**/*.ico":true,
"**/*.jpg":true,
"**/*.jpeg":true,
"**/*.png":true,
"**/*.psd":true,
"**/*.tga":true,
"**/*.tif":true,
"**/*.tiff":true,
"**/*.3ds":true,
"**/*.3DS":true,
"**/*.fbx":true,
"**/*.FBX":true,
"**/*.lxo":true,
"**/*.LXO":true,
"**/*.ma":true,
"**/*.MA":true,
"**/*.obj":true,
"**/*.OBJ":true,
"**/*.asset":true,
"**/*.cubemap":true,
"**/*.flare":true,
"**/*.mat":true,
"**/*.meta":true,
"**/*.prefab":true,
"**/*.unity":true,
"build/":true,
"Build/":true,
"Library/":true,
"library/":true,
"obj/":true,
"Obj/":true,
"ProjectSettings/":true,
"temp/":true,
"Temp/":true
}
}
92 changes: 92 additions & 0 deletions Assets/Scenes/MainGame.unity
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,7 @@ GameObject:
- component: {fileID: 519420032}
- component: {fileID: 519420031}
- component: {fileID: 519420029}
- component: {fileID: 519420030}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
Expand All @@ -999,6 +1000,20 @@ AudioListener:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 519420028}
m_Enabled: 1
--- !u!114 &519420030
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 519420028}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ebec9565d20124607bcabecf60efe305, type: 3}
m_Name:
m_EditorClassIdentifier:
healthText: {fileID: 1824982354}
deathPanel: {fileID: 586967968}
--- !u!20 &519420031
Camera:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1056,6 +1071,81 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &586967968
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 586967969}
- component: {fileID: 586967971}
- component: {fileID: 586967970}
m_Layer: 5
m_Name: DeathPanel
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!224 &586967969
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 586967968}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 729533674}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &586967970
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 586967968}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 0, b: 0, a: 0.5254902}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_Sprite: {fileID: 10907, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!222 &586967971
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 586967968}
m_CullTransparentMesh: 0
--- !u!1 &660912747
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -6061,6 +6151,7 @@ RectTransform:
m_Children:
- {fileID: 245587107}
- {fileID: 1824982353}
- {fileID: 586967969}
m_Father: {fileID: 0}
m_RootOrder: 7
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down Expand Up @@ -6301,6 +6392,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
speed: 10
health: 3
deathPanel: {fileID: 586967968}
deathFX: {fileID: 1022081403356353717, guid: 7fb71251b4c62d24196d8c14af728473, type: 3}
--- !u!61 &1013810065
BoxCollider2D:
Expand Down
43 changes: 43 additions & 0 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

//using a singleton style class (in the unity way)
public class GameManager : MonoBehaviour
{
static GameManager _instance=null;

public Text healthText;
public GameObject deathPanel;

void Awake(){
if(_instance==null){
_instance=this;
}
}

public static GameManager instance(){
return _instance;
}

public void updateHealthText(int health){
healthText.text="x"+health;
}

public void deathPanelSwich(bool state){
deathPanel.SetActive(state);
}

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/GameManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions Assets/Scripts/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class Player : MonoBehaviour

private float eps = 0.0001f;

public Text lifeText;
//FIXME: I would much rather this be in a gamemanager class, not in the player class
public GameObject deathPanel;

//death fx
public GameObject deathFX;
Expand All @@ -26,7 +27,9 @@ void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
lifeText.text="x"+health;
GameManager.instance().updateHealthText(health);
GameManager.instance().deathPanelSwich(false);

}

private void Update() {
Expand Down Expand Up @@ -57,11 +60,14 @@ void FixedUpdate() {

public void takeDamage(int value) {
health -= value;
lifeText.text="x"+health;
GameManager.instance().updateHealthText(health);

if (health <= 0) {
//player dies
Instantiate(deathFX, new Vector3(transform.position.x, transform.position.y, 0f), Quaternion.identity);
GameObject.Destroy(gameObject);
GameManager.instance().deathPanelSwich(true);

}
}
}

0 comments on commit 50d617c

Please sign in to comment.