我们在v0.0.2规划中有:

  • 统计系统:统计击杀敌人的数量
  • 主角的Model:主角的生命值信息
  • 主角碰到敌人就掉血
  • 关卡中的通关功能(终点)
  • 摄像机跟随主角功能

我们这里先实现统计系统和主角的Model

在ShootingEditor2D中引入框架

首先整理一下之前的脚本文件,我们在Scripts文件夹中新建ViewController、Model、System、Command文件夹,并把之前的脚本都放入ViewController文件夹里面

整理文件结构

上面截图中还没有创建Command文件夹,注意创建

然后在Script文件夹内新建脚本,命名为ShootingEditor2D,作为引入框架的主脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
using FrameWorkDesign;

namespace ShootingEditor2D
{
public class ShootingEditor2D : Architecture<ShootingEditor2D>
{
protected override void Init()
{
this.RegisterSystem<IStatSystem>(new StatSystem());
this.RegisterModel<IPlayerModel>(new PlayerModel());
}
}
}

在System文件夹内新建IStatSystem,即统计系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using FrameWorkDesign;

namespace ShootingEditor2D
{
public interface IStatSystem : ISystem
{
BindableProperty<int> KillCount { get; }
}

public class StatSystem : AbstractSystem, IStatSystem
{
//千万不要写成public BindableProperty<int> KillCount => new BindableProperty<int>() { Value = 0};
//这样写相当于public BindableProperty<int> KillCount { get { return new BindableProperty<int>();}};
//这样KillCount的数据就无法修改了
public BindableProperty<int> KillCount { get; } = new BindableProperty<int>() { Value = 0};

protected override void OnInit()
{

}
}
}

在Model文件夹内新建IPlayerModel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using FrameWorkDesign;

namespace ShootingEditor2D
{
public interface IPlayerModel : IModel
{
BindableProperty<int> HP { get; }
}

public class PlayerModel : AbstractModel, IPlayerModel
{
public BindableProperty<int> HP { get; } = new BindableProperty<int>() { Value = 3};

protected override void OnInit()
{

}
}
}

实现击杀敌人数量统计功能

纸上设计

击杀敌人统计

这里使用IMGUI的方式实现击杀敌人数量的显示

在Hierarchy面板新建空对象,命名为UI

在ViewController文件夹中新建脚本UIController,挂载在UI上

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
using UnityEngine;
using FrameWorkDesign;
using System;

namespace ShootingEditor2D
{
public class UIController : MonoBehaviour,IController
{
private IStatSystem m_StatSystem;
private IPlayerModel m_PlayerModel;

private void Awake()
{
m_PlayerModel = this.GetModel<IPlayerModel>();
m_StatSystem = this.GetSystem<IStatSystem>();
}

private readonly Lazy<GUIStyle> m_LableStyle = new Lazy<GUIStyle>(() =>
{
return new GUIStyle(GUI.skin.label)
{
fontSize = 48//将字体改大一些
};
});

private void OnGUI()
{
GUI.Label(new Rect(10, 10, 300, 100), $"生命:{m_PlayerModel.HP.Value}/3",m_LableStyle.Value);
GUI.Label(new Rect(Screen.width - 10 - 300, 10, 300, 100), $"击杀数量:{m_StatSystem.KillCount.Value}",m_LableStyle.Value);
}
public IArchitecture GetArchitecture()
{
return ShootingEditor2D.Interface;
}


}
}

在上面的代码中,我们使用了Lazy类来懒初始化GUIStyle,因为GUIStyle只能在OnGUI的生命周期内初始化,这是一个将GUI代码放在OnGUI方法之外的一个技巧

实现增加击杀次数的功能

在Command文件夹内创建KillEnemyCommand脚本

1
2
3
4
5
6
7
8
9
10
11
12
using FrameWorkDesign;

namespace ShootingEditor2D
{
public class KillEnemyCommand : AbstractCommand
{
protected override void OnExecute()
{
this.GetSystem<IStatSystem>().KillCount.Value++;
}
}
}

修改Bullet脚本

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
using UnityEngine;
using FrameWorkDesign;

namespace ShootingEditor2D
{
public class Bullet : MonoBehaviour,IController//
{
private Rigidbody2D m_Rigidbody;

private void Awake()
{
m_Rigidbody = GetComponent<Rigidbody2D>();
}

private void Start()
{
m_Rigidbody.velocity = Vector2.right * 10;
}

private void OnCollisionEnter2D(Collision2D collision)
{
this.SendCommand<KillEnemyCommand>();//

if(collision.gameObject.CompareTag("Enemy"))
Destroy(collision.gameObject);
}

public IArchitecture GetArchitecture()//
{
return ShootingEditor2D.Interface;
}
}
}

这样功能就实现好了

击杀显示