Added Revive state and rewarded ad

Tahir-branch
Hazim Bin Ijaz 2 months ago
parent 0f798993bc
commit 5a31274499

@ -32,7 +32,7 @@ namespace D2D.Core
On<WinState>(OnGameWin); On<WinState>(OnGameWin);
On<LoseState>(OnGameLose); On<LoseState>(OnGameLose);
On<SceneLoading>(OnPostgame); On<SceneLoading>(OnPostgame);
On<RevivalState>(OnRevivalState);
On<WinState, LoseState>(OnGameFinish); On<WinState, LoseState>(OnGameFinish);
On<CustomStateA>(OnCustomStateA); On<CustomStateA>(OnCustomStateA);
@ -55,6 +55,7 @@ namespace D2D.Core
protected virtual void OnPostgame() { } protected virtual void OnPostgame() { }
protected virtual void OnGameFinish() { } protected virtual void OnGameFinish() { }
protected virtual void OnRevivalState() { }
protected void On<TState>(Action a) where TState : GameState protected void On<TState>(Action a) where TState : GameState
{ {

@ -32,6 +32,7 @@ namespace D2D.Core
public class WinState : GameState { } public class WinState : GameState { }
public class SceneLoading : GameState { } public class SceneLoading : GameState { }
public class RevivalState : GameState { }
// ------------------------------------------ // ------------------------------------------

@ -36,6 +36,7 @@ namespace D2D.Gameplay
} }
ShowProperty("_isGrayFadeout", "Is Gray Fadeout"); ShowProperty("_isGrayFadeout", "Is Gray Fadeout");
ShowProperty("_isPlayer", "Is Player");
EndSerialization(); EndSerialization();
} }

@ -1,9 +1,9 @@
using System; using System;
using System.Collections;
using D2D.Core;
using D2D.Utilities; using D2D.Utilities;
using DG.Tweening; using DG.Tweening;
using UnityEngine; using UnityEngine;
using static D2D.Utilities.SettingsFacade;
using static D2D.Utilities.CommonLazyFacade;
using static D2D.Utilities.CommonGameplayFacade; using static D2D.Utilities.CommonGameplayFacade;
namespace D2D.Gameplay namespace D2D.Gameplay
@ -22,7 +22,8 @@ namespace D2D.Gameplay
[SerializeField] private GameObject _deathEffect; [SerializeField] private GameObject _deathEffect;
[SerializeField] private bool _isGrayFadeout; [SerializeField] private bool _isGrayFadeout;
[SerializeField] private bool _isPlayer;
public event Action Died; public event Action Died;
public event Action<float> Damaged; public event Action<float> Damaged;
public event Action PointsChanged; public event Action PointsChanged;
@ -31,6 +32,17 @@ namespace D2D.Gameplay
public GameObject LastAttacker { get; private set; } public GameObject LastAttacker { get; private set; }
private void OnEnable()
{
AdsManager.Instance.OnUserEarnedReward += OnRewardsAchieved;
}
private void OnDisable()
{
AdsManager.Instance.OnUserEarnedReward -= OnRewardsAchieved;
}
public float CurrentPoints public float CurrentPoints
{ {
get => _currentPoints; get => _currentPoints;
@ -112,18 +124,57 @@ namespace D2D.Gameplay
} }
else else
{ {
Died?.Invoke(); if (_isPlayer && AdsManager.Instance.isRewardedAdAvailable)
if (_isGrayFadeout)
{ {
GrayFadeoutDeath(); _stateMachine.Push(new RevivalState());
deathCoroutine = StartCoroutine(DeathCoroutine());
} }
else else
{ {
ImmediateDeath(); InitiateDeathSequence();
} }
} }
} }
private Coroutine deathCoroutine;
private void OnRewardsAchieved(RewardedTypes rewardedTypes)
{
if (_isPlayer)
{
if (rewardedTypes != RewardedTypes.Revive) return;
StopCoroutine(deathCoroutine);
CurrentPoints = MaxPoints;
_stateMachine.Push(new RunningState());
GameManager.Instance.OnRevival();
Debug.Log("Player is Revived!!!");
}
else
{
InitiateDeathSequence();
}
}
IEnumerator DeathCoroutine()
{
yield return new WaitForSeconds(10f);
InitiateDeathSequence();
}
private void InitiateDeathSequence()
{
Died?.Invoke();
if (_isGrayFadeout)
{
GrayFadeoutDeath();
}
else
{
ImmediateDeath();
}
}
private void ImmediateDeath() private void ImmediateDeath()
{ {

@ -13,6 +13,7 @@ namespace D2D
public GameObject[] _onWin; public GameObject[] _onWin;
public GameObject[] _onLose; public GameObject[] _onLose;
public GameObject[] _onGameFinish; public GameObject[] _onGameFinish;
public GameObject[] _onReviveOption;
protected override void OnGameRun() => Show(_onRunning); protected override void OnGameRun() => Show(_onRunning);
@ -21,6 +22,7 @@ namespace D2D
protected override void OnGameLose() => Show(_onLose); protected override void OnGameLose() => Show(_onLose);
protected override void OnGameWin() => Show(_onWin); protected override void OnGameWin() => Show(_onWin);
protected override void OnRevivalState() => Show(_onReviveOption);
private void Show(GameObject[] parent) private void Show(GameObject[] parent)
{ {

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

@ -543,6 +543,53 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 67db9e8f0e2ae9c40bc1e2b64352a6b4, type: 3} m_Script: {fileID: 11500000, guid: 67db9e8f0e2ae9c40bc1e2b64352a6b4, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
--- !u!1 &747682101
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 747682103}
- component: {fileID: 747682102}
m_Layer: 0
m_Name: AdsManager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &747682102
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 747682101}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a853c83bcccfa14468a366e0290483a3, type: 3}
m_Name:
m_EditorClassIdentifier:
iAdUnit: ca-app-pub-3940256099942544/1033173712
rAdUnit: ca-app-pub-3940256099942544/5224354917
isRewardedAdAvailable: 0
--- !u!4 &747682103
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 747682101}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 498.7936, y: 630.3509, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &794248333 --- !u!1 &794248333
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

@ -31,7 +31,7 @@ public class AdsManager : MonoBehaviour
private bool loadingrAd = false; private bool loadingrAd = false;
private RewardedTypes rewardType; private RewardedTypes rewardType;
private bool isRewardAvailable = false; private bool isRewardAvailable = false;
public static AdsManager Instance; public static AdsManager Instance;
public Action<RewardedTypes> OnUserEarnedReward; public Action<RewardedTypes> OnUserEarnedReward;
public Action OnAdmobInitializedEvent; public Action OnAdmobInitializedEvent;
@ -40,12 +40,16 @@ public class AdsManager : MonoBehaviour
public Action<bool> OniAdLoaded; public Action<bool> OniAdLoaded;
public Action<bool> OnrAdLoaded; public Action<bool> OnrAdLoaded;
private void Awake() private void Awake()
{ {
if (Instance is null) if (Instance != null && Instance != this)
Instance = this; {
else Destroy(this);
DestroyImmediate(this); }
else
{
Instance = this;
}
DontDestroyOnLoad(gameObject); DontDestroyOnLoad(gameObject);
} }
@ -263,6 +267,7 @@ public class AdsManager : MonoBehaviour
RewardedAd.Load(rAdUnit, adRequest, OnRewardedAdLoaded); RewardedAd.Load(rAdUnit, adRequest, OnRewardedAdLoaded);
} }
public bool isRewardedAdAvailable = false;
private void OnRewardedAdLoaded(RewardedAd ad, LoadAdError error) private void OnRewardedAdLoaded(RewardedAd ad, LoadAdError error)
{ {
MobileAdsEventExecutor.ExecuteInUpdate(() => MobileAdsEventExecutor.ExecuteInUpdate(() =>
@ -273,6 +278,7 @@ public class AdsManager : MonoBehaviour
if (error != null || ad == null) if (error != null || ad == null)
{ {
OnrAdLoaded?.Invoke(false); OnrAdLoaded?.Invoke(false);
isRewardedAdAvailable = false;
rewardedRetryAttempt++; rewardedRetryAttempt++;
//load ad again //load ad again
LoadRewardedAdWithDelay(); LoadRewardedAdWithDelay();
@ -285,6 +291,7 @@ public class AdsManager : MonoBehaviour
Debug.Log("rewarded ad loaded with response : " Debug.Log("rewarded ad loaded with response : "
+ ad.GetResponseInfo()); + ad.GetResponseInfo());
isRewardedAdAvailable = true;
OnrAdLoaded?.Invoke(true); OnrAdLoaded?.Invoke(true);
_rewardedAd = ad; _rewardedAd = ad;
RegisterEventHandlers(_rewardedAd); RegisterEventHandlers(_rewardedAd);

@ -0,0 +1,29 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager Instance = null;
private void Awake()
{
if (Instance is null)
Instance = this;
else
{
DestroyImmediate(this);
}
}
public GameObject ReviveCanvas;
public void OnRevival()
{
ReviveCanvas.gameObject.SetActive(false);
}
}

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 15aa547f90790934786675afd937e396
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,37 @@
using System.Collections;
using System.Collections.Generic;
using D2D.UI;
using D2D.Utilities;
using UnityEngine;
public class ReviveOnClick : DButtonListener
{
[SerializeField] private bool _isButton;
protected bool Was { get; private set; }
private bool _isClaimClickMissed;
private void Update()
{
if (!_isButton && DInput.IsMouseReleased)
OnClick();
}
protected override void OnClick()
{
if (Was)
return;
Was = true;
ShowRewardedAd();
}
private void ShowRewardedAd()
{
AdsManager.Instance.ShowRewardedAd(RewardedTypes.Revive);
}
}

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b96eae8828f21dd4aaadd5c22d8bf37d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,13 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReviveScreenUI : MonoBehaviour
{
public GameObject ReviveButton;
private void OnEnable()
{
ReviveButton.SetActive(AdsManager.Instance.isRewardedAdAvailable);
}
}

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4a6125f6ff849814c8d7e8cdb145134a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -518,6 +518,7 @@ MonoBehaviour:
_hitEffect: {fileID: 0} _hitEffect: {fileID: 0}
_deathEffect: {fileID: 193062, guid: 01949c12a89de284792dc4b7b7128404, type: 3} _deathEffect: {fileID: 193062, guid: 01949c12a89de284792dc4b7b7128404, type: 3}
_isGrayFadeout: 0 _isGrayFadeout: 0
_isPlayer: 1
isHealthDataMode: 0 isHealthDataMode: 0
particlesAreEnabled: 1 particlesAreEnabled: 1
--- !u!54 &2670091936408417004 --- !u!54 &2670091936408417004

@ -235,6 +235,7 @@ MonoBehaviour:
_hitEffect: {fileID: 0} _hitEffect: {fileID: 0}
_deathEffect: {fileID: 193062, guid: 01949c12a89de284792dc4b7b7128404, type: 3} _deathEffect: {fileID: 193062, guid: 01949c12a89de284792dc4b7b7128404, type: 3}
_isGrayFadeout: 0 _isGrayFadeout: 0
_isPlayer: 1
isHealthDataMode: 0 isHealthDataMode: 0
particlesAreEnabled: 1 particlesAreEnabled: 1
--- !u!54 &2670091936408417004 --- !u!54 &2670091936408417004

Loading…
Cancel
Save