-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathFoodTextUpdateSystem.cs
79 lines (70 loc) · 2.48 KB
/
FoodTextUpdateSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Linq;
using SystemsRx.Attributes;
using SystemsRx.Events;
using SystemsRx.Extensions;
using SystemsRx.Systems.Conventional;
using EcsRx.Collections;
using EcsRx.Extensions;
using EcsRx.Groups;
using EcsRx.Groups.Observable;
using EcsRx.Plugins.GroupBinding.Attributes;
using EcsRx.Systems;
using EcsRx.Unity.Extensions;
using Game.Components;
using Game.Events;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
namespace Game.Systems
{
[Priority(10)]
public class FoodTextUpdateSystem : IManualSystem, IGroupSystem
{
public IGroup Group { get; } = new Group(typeof(PlayerComponent));
[FromGroup]
public IObservableGroup ObservableGroup;
private readonly IEventSystem _eventSystem;
private PlayerComponent _playerComponent;
private Text _foodText;
private readonly IList<IDisposable> _subscriptions = new List<IDisposable>();
public FoodTextUpdateSystem(IEventSystem eventSystem)
{
_eventSystem = eventSystem;
}
public void StartSystem()
{
this.WaitForScene().Subscribe(x =>
{
var player = ObservableGroup.First();
_playerComponent = player.GetComponent<PlayerComponent>();
_foodText = GameObject.Find("FoodText").GetComponent<Text>();
SetupSubscriptions();
});
}
private void SetupSubscriptions()
{
_playerComponent.Food.DistinctUntilChanged()
.Subscribe(foodAmount => { _foodText.text = $"Food: {foodAmount}"; })
.AddTo(_subscriptions);
_eventSystem.Receive<FoodPickupEvent>()
.Subscribe(x =>
{
var foodComponent = x.Food.GetComponent<FoodComponent>();
var foodPoints = foodComponent.FoodAmount;
_foodText.text = $"+{foodPoints} Food: {_playerComponent.Food.Value}";
})
.AddTo(_subscriptions);
_eventSystem.Receive<PlayerHitEvent>()
.Subscribe(x =>
{
var attackScore = x.Enemy.GetComponent<EnemyComponent>().EnemyPower;
_foodText.text = $"-{attackScore} Food: {_playerComponent.Food.Value}";
})
.AddTo(_subscriptions);
}
public void StopSystem()
{ _subscriptions.DisposeAll(); }
}
}