22.PureMVC的Model和Proxy

22.PureMVC框架-Model数据层和Proxy代理模式


22.1 知识点

我们不会直接创建Model,而是创建DataObject然后通过Proxy进行管理

创建玩家数据结构对象脚本,声明玩家相关的变量

/// <summary>
/// 玩家数据结构 
/// </summary>
public class PlayerDataObj
{
    //申明一堆玩家属性相关的变量
    public string playerName;
    public int lev;
    public int money;
    public int gem;
    public int power;
    
    public int hp;
    public int atk;
    public int def;
    public int crit;
    public int miss;
    public int luck;
}

创建玩家数据代理脚本,继承Proxy

/// <summary>
/// 玩家数据代理对象
/// 主要处理 玩家数据更新相关的逻辑
/// </summary>
public class PlayerProxy : Proxy
{
}

玩家数据代理中定义新的代理名。继承父类有一个代理名参数的构造函数。构造函数中实例化一个玩家数据并设置初始值赋值给父类Data属性。

//重写名字作为代理名
public new const string NAME = "PlayerProxy";

//写构造函数
//重要点
//1.代理的名字!!!!
//2.代理相关的数据!!!!!

//构造函数要继承父类有一个代理名参数的构造函数 也可以外部传入data
public PlayerProxy():base(PlayerProxy.NAME)
{
    //在构造函数中 初始化一个数据 进行关联
    PlayerDataObj data = new PlayerDataObj();
    
    //初始化
    data.playerName = PlayerPrefs.GetString("PlayerName", "韬老狮");
    data.lev = PlayerPrefs.GetInt("PlayerLev", 1);
    data.money = PlayerPrefs.GetInt("PlayerMoney", 9999);
    data.gem = PlayerPrefs.GetInt("PlayerGem", 8888);
    data.power = PlayerPrefs.GetInt("PlayerPower", 99);
    
    data.hp = PlayerPrefs.GetInt("PlayerHp", 100);
    data.atk = PlayerPrefs.GetInt("PlayerAtk", 20);
    data.def = PlayerPrefs.GetInt("PlayerDef", 10);
    data.crit = PlayerPrefs.GetInt("PlayerCrit", 20);
    data.miss = PlayerPrefs.GetInt("PlayerMiss", 10);
    data.luck = PlayerPrefs.GetInt("PlayerLuck", 40);
    
    //赋值给自己的Data进行关联 Data是父类的属性 要在构造函数被赋值 外部使用代理时使用的就是这个大Data
    Data = data;
}

定义升级方法和存储数据方法。要先把大Data转存一遍进行操作。

public void LevUp()
{
    PlayerDataObj data = Data as PlayerDataObj;
    
    //升级 改变内容
    data.lev += 1;
    
    data.hp += data.lev;
    data.atk += data.lev;
    data.def += data.lev;
    data.crit += data.lev;
    data.miss += data.lev;
    data.luck += data.lev;
}

public void SaveData()
{
    PlayerDataObj data = Data as PlayerDataObj;
    //把这些数据内容 存储到本地
    PlayerPrefs.SetString("PlayerName", data.playerName);
    PlayerPrefs.SetInt("PlayerLev", data.lev);
    PlayerPrefs.SetInt("PlayerMoney", data.money);
    PlayerPrefs.SetInt("PlayerGem", data.gem);
    PlayerPrefs.SetInt("PlayerPower", data.power);
    
    PlayerPrefs.SetInt("PlayerHp", data.hp);
    PlayerPrefs.SetInt("PlayerAtk", data.atk);
    PlayerPrefs.SetInt("PlayerDef", data.def);
    PlayerPrefs.SetInt("PlayerCrit", data.crit);
    PlayerPrefs.SetInt("PlayerMiss", data.miss);
    PlayerPrefs.SetInt("PlayerLuck", data.luck);
}

22.2 知识点代码

PlayerDataObj

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 玩家数据结构 
/// </summary>
public class PlayerDataObj
{
    //申明一堆玩家属性相关的变量
    public string playerName;
    public int lev;
    public int money;
    public int gem;
    public int power;

    public int hp;
    public int atk;
    public int def;
    public int crit;
    public int miss;
    public int luck;
}

PlayerProxy

using PureMVC.Patterns.Proxy;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 玩家数据代理对象
/// 主要处理 玩家数据更新相关的逻辑
/// </summary>
public class PlayerProxy : Proxy
{
    //1.继承Proxy父类
    //2.写我们的构造函数

    //重写名字作为代理名
    public new const string NAME = "PlayerProxy";

    //写构造函数
    //重要点
    //1.代理的名字!!!!
    //2.代理相关的数据!!!!!

    //构造函数要继承父类有一个代理名参数的构造函数 也可以外部传入data
    public PlayerProxy():base(PlayerProxy.NAME)
    {
        //在构造函数中 初始化一个数据 进行关联
        PlayerDataObj data = new PlayerDataObj();

        //初始化
        data.playerName = PlayerPrefs.GetString("PlayerName", "韬老狮");
        data.lev = PlayerPrefs.GetInt("PlayerLev", 1);
        data.money = PlayerPrefs.GetInt("PlayerMoney", 9999);
        data.gem = PlayerPrefs.GetInt("PlayerGem", 8888);
        data.power = PlayerPrefs.GetInt("PlayerPower", 99);

        data.hp = PlayerPrefs.GetInt("PlayerHp", 100);
        data.atk = PlayerPrefs.GetInt("PlayerAtk", 20);
        data.def = PlayerPrefs.GetInt("PlayerDef", 10);
        data.crit = PlayerPrefs.GetInt("PlayerCrit", 20);
        data.miss = PlayerPrefs.GetInt("PlayerMiss", 10);
        data.luck = PlayerPrefs.GetInt("PlayerLuck", 40);

        //赋值给自己的Data进行关联 Data是父类的属性 要在构造函数被赋值 外部使用代理时使用的就是这个大Data
        Data = data;
    }
    public void LevUp()
    {
        PlayerDataObj data = Data as PlayerDataObj;

        //升级 改变内容
        data.lev += 1;

        data.hp += data.lev;
        data.atk += data.lev;
        data.def += data.lev;
        data.crit += data.lev;
        data.miss += data.lev;
        data.luck += data.lev;
    }

    public void SaveData()
    {
        PlayerDataObj data = Data as PlayerDataObj;
        //把这些数据内容 存储到本地
        PlayerPrefs.SetString("PlayerName", data.playerName);
        PlayerPrefs.SetInt("PlayerLev", data.lev);
        PlayerPrefs.SetInt("PlayerMoney", data.money);
        PlayerPrefs.SetInt("PlayerGem", data.gem);
        PlayerPrefs.SetInt("PlayerPower", data.power);

        PlayerPrefs.SetInt("PlayerHp", data.hp);
        PlayerPrefs.SetInt("PlayerAtk", data.atk);
        PlayerPrefs.SetInt("PlayerDef", data.def);
        PlayerPrefs.SetInt("PlayerCrit", data.crit);
        PlayerPrefs.SetInt("PlayerMiss", data.miss);
        PlayerPrefs.SetInt("PlayerLuck", data.luck);
    }

}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏