对象
何为对象,套用一句话,一切皆对象,那么,在游戏里,我们需要将那些对象进行管理呢?
我认为,在游戏里主要管理的对象有以下几个
- 主观存在
- 建筑
- 人物
- 物品
- 资源
- 客观存在
- 技能
- 效果
- 奖励
- 进度(关卡等等)
也就是说,我们还需要做出以上几个的通用类。
首先,让我们给这些对象套上一个统一的壳子。
我们这里起名叫Active,代表活跃的,存活的、
对象
using UnityEngine;
using System.Collections;
public class Active : MonoBehaviour {
protected int type; // 类型
public virtual int getType() {
return type;
}
// 做一些事情(循环执行,类似心跳)
public virtual void doThing() {
}
}
接着做主观存在的
主观存在
其中nature为属性池,配合NumberGet即可实现二次编程。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameNpc : Active {
//基础属性
[SerializeField, HeaderAttribute("-----基础属性------"), Space(15), TooltipAttribute("id[唯一性]")]
int id;//id,由系统生成
public int Id {
get {
return (int)getNature("id", id);
}
set {
id = (int)setNature("id", value);
}
}
[SerializeField]
string npcName;//姓名,玩家自己填写
public string NpcName {
get {
return (string)getNature("npcName", npcName);
}
set {
npcName = (string)setNature("npcName", value);
}
}
[SerializeField]
float hp;//生命
public float Hp {
get {
return (float)getNature("hp", hp);
}
set {
hp = (float)setNature("hp", value);
}
}
[SerializeField]
float maxHp = 10f;//最大生命
public float MaxHp {
get {
return (float)getNature("maxHp", maxHp);
}
set {
maxHp = (float)setNature("maxHp", value);
}
}
[SerializeField]
float mp;//法力
public float Mp {
get {
return (float)getNature("maxHp", maxHp);
}
set {
maxHp = (float)setNature("maxHp", value);
}
}
[SerializeField]
float maxMp = 2f;//最大法力
public float MaxMp {
get {
return (float)getNature("maxHp", maxHp);
}
set {
maxHp = (float)setNature("maxHp", value);
}
}
[SerializeField]
float pAttLow = 1f;//物理攻击下限
public float PAttLow {
get {
return (float)getNature("pAttLow", pAttLow);
}
set {
pAttLow = (float)setNature("pAttLow", value);
}
}
[SerializeField]
float pAttHigh = 3f;//物理攻击上限
public float PAttHigh {
get {
return (float)getNature("pAttHigh", pAttHigh);
}
set {
pAttHigh = (float)setNature("pAttHigh", value);
}
}
[SerializeField]
float mAttLow = 1f;//法术攻击下限
public float MAttLow {
get {
return (float)getNature("mAttLow", mAttLow);
}
set {
mAttLow = (float)setNature("mAttLow", value);
}
}
[SerializeField]
float mAttHigh = 1f;//法术攻击上限
public float MAttHigh {
get {
return (float)getNature("mAttHigh", mAttHigh);
}
set {
mAttHigh = (float)setNature("mAttHigh", value);
}
}
[SerializeField]
float pDefPower = 1f;//物理防御
public float PDefPower {
get {
return (float)getNature("pDefPower", pDefPower);
}
set {
pDefPower = (float)setNature("pDefPower", value);
}
}
[SerializeField]
float mDefPower = 0f;//法术防御
public float MDefPower {
get {
return (float)getNature("mDefPower", mDefPower);
}
set {
mDefPower = (float)setNature("mDefPower", value);
}
}
[SerializeField]
int lvl = 1;//等级
public int Lvl {
get {
return (int)getNature("lvl", lvl);
}
set {
lvl = (int)setNature("lvl", value);
}
}
[SerializeField]
int maxLvl = 10;//最大等级
public int MaxLvl {
get {
return (int)getNature("maxLvl", maxLvl);
}
set {
maxLvl = (int)setNature("maxLvl", value);
}
}
[SerializeField]
float exp = 0f;//经验
public float Exp {
get {
return (float)getNature("exp", exp);
}
set {
exp = (float)setNature("exp", value);
}
}
[SerializeField]
float maxExp = 100;//最大经验
public float MaxExp {
get {
return (float)getNature("maxExp", maxExp);
}
set {
maxExp = (float)setNature("maxExp", value);
}
}
[SerializeField, Range(0, 100)]
float dropExpOdds = 0;//经验掉落率
public float DropExpOdds {
get {
return (float)getNature("dropExpOdds", dropExpOdds);
}
set {
dropExpOdds = (float)setNature("dropExpOdds", value);
}
}
[SerializeField]
float money = 50f;//金钱
public float Money {
get {
return (float)getNature("money", money);
}
set {
money = (float)setNature("money", value);
}
}
[SerializeField]
float maxMoney = 10000f;//最大金钱
public float MaxMoney {
get {
return (float)getNature("maxMoney", maxMoney);
}
set {
maxMoney = (float)setNature("maxMoney", value);
}
}
[SerializeField]
float diamond = 50f;//钻石
public float Diamond {
get {
return (float)getNature("diamond", diamond);
}
set {
diamond = (float)setNature("diamond", value);
}
}
[SerializeField]
float maxDiamond = 10000f;//最大钻石
public float MaxDiamond {
get {
return (float)getNature("maxDiamond", maxDiamond);
}
set {
maxDiamond = (float)setNature("maxDiamond", value);
}
}
[SerializeField, Range(0, 100)]
float dropMoneyOdds = 0f;//金钱掉落率
public float DropMoneyOdds {
get {
return (float)getNature("dropMoneyOdds", dropMoneyOdds);
}
set {
dropMoneyOdds = (float)setNature("dropMoneyOdds", value);
}
}
[SerializeField]
float norFightSpeed = 2.5f;//基础攻击速度
public float NorFightSpeed {
get {
return (float)getNature("norFightSpeed", norFightSpeed);
}
set {
norFightSpeed = (float)setNature("norFightSpeed", value);
}
}
[SerializeField]
float fightSpeedAmp = 0f;//攻击速度加成
public float FightSpeedAmp {
get {
return (float)getNature("fightSpeedAmp", fightSpeedAmp);
}
set {
fightSpeedAmp = (float)setNature("fightSpeedAmp", value);
}
}
[SerializeField, MultilineAttribute(5)]
string conText;//介绍
public string ConText {
get {
return (string)getNature("conText", conText);
}
set {
conText = (string)setNature("conText", value);
}
}
//特殊属性
[SerializeField, HeaderAttribute("------特殊属性------"), Space(15), Range(0, 100)]
float pVioHurtOdds = 0f;//物理暴击概率
public float PVioHurtOdds {
get {
return (float)getNature("pVioHurtOdds", pVioHurtOdds);
}
set {
pVioHurtOdds = (float)setNature("pVioHurtOdds", value);
}
}
[SerializeField]
float pVioHurtAmp = 50f;//物理暴击伤害增幅
public float PVioHurtAmp {
get {
return (float)getNature("pVioHurtAmp", pVioHurtAmp);
}
set {
pVioHurtAmp = (float)setNature("pVioHurtAmp", value);
}
}
[SerializeField]
float pVioHurtAdd = 0f;//物理暴击伤害附加'
public float PVioHurtAdd {
get {
return (float)getNature("pVioHurtAdd", pVioHurtAdd);
}
set {
pVioHurtAdd = (float)setNature("pVioHurtAdd", value);
}
}
[SerializeField, Range(0, 100)]
float pVioHurtMissDefOdds = 0f;//物理暴击时无视防御力概率
public float PVioHurtMissDefOdds {
get {
return (float)getNature("pVioHurtMissDefOdds", pVioHurtMissDefOdds);
}
set {
pVioHurtMissDefOdds = (float)setNature("pVioHurtMissDefOdds", value);
}
}
[SerializeField]
float pVioHurtMissDefAmp = 0f;//物理暴击时无视防御力增幅
public float PVioHurtMissDefAmp {
get {
return (float)getNature("pVioHurtMissDefAmp", pVioHurtMissDefAmp);
}
set {
pVioHurtMissDefAmp = (float)setNature("pVioHurtMissDefAmp", value);
}
}
[SerializeField]
float pVioHurtMissDefAdd = 0f;//物理暴击时无视防御力附加
public float PVioHurtMissDefAdd {
get {
return (float)getNature("pVioHurtMissDefAdd", pVioHurtMissDefAdd);
}
set {
pVioHurtMissDefAdd = (float)setNature("pVioHurtMissDefAdd", value);
}
}
[SerializeField, Range(0, 100)]
float doeOdds = 0f;//闪避几率
public float DoeOdds {
get {
return (float)getNature("doeOdds", doeOdds);
}
set {
doeOdds = (float)setNature("doeOdds", value);
}
}
[SerializeField]
float recoverHpPerSecond = 0f;//每秒回复生命
public float RecoverHpPerSecond {
get {
return (float)getNature("recoverHpPerSecond", recoverHpPerSecond);
}
set {
recoverHpPerSecond = (float)setNature("recoverHpPerSecond", value);
}
}
[SerializeField]
float recoverMpPerSecond = 0f;//每秒回复法力
public float RecoverMpPerSecond {
get {
return (float)getNature("recoverMpPerSecond", recoverMpPerSecond);
}
set {
recoverMpPerSecond = (float)setNature("recoverMpPerSecond", value);
}
}
[SerializeField]
float recoverHpPerHit = 0f;//击中回复生命
public float RecoverHpPerHit {
get {
return (float)getNature("recoverHpPerHit", recoverHpPerHit);
}
set {
recoverHpPerHit = (float)setNature("recoverHpPerHit", value);
}
}
[SerializeField]
float recoverMpPerHit = 0f;//击中回复法力
public float RecoverMpPerHit {
get {
return (float)getNature("recoverMpPerHit", recoverMpPerHit);
}
set {
recoverMpPerHit = (float)setNature("recoverMpPerHit", value);
}
}
[SerializeField]
float extraItemOdds = 0f;//物品掉落加成
public float ExtraItemOdds {
get {
return (float)getNature("extraItemOdds", extraItemOdds);
}
set {
extraItemOdds = (float)setNature("extraItemOdds", value);
}
}
[SerializeField]
float extraExpOdds = 0f;//经验掉落加成
public float ExtraExpOdds {
get {
return (float)getNature("extraExpOdds", extraExpOdds);
}
set {
extraExpOdds = (float)setNature("extraExpOdds", value);
}
}
[SerializeField]
float extraMoneyOdds = 0f;//金钱掉落加成
public float ExtraMoneyOdds {
get {
return (float)getNature("extraMoneyOdds", extraMoneyOdds);
}
set {
extraMoneyOdds = (float)setNature("extraMoneyOdds", value);
}
}
[SerializeField, Range(0, 100)]
float autoHeal = 0;//附加回血百分比
public float AutoHeal {
get {
return (float)getNature("autoHeal", autoHeal);
}
set {
autoHeal = (float)setNature("autoHeal", value);
}
}
[SerializeField, Range(0, 100)]
float autoMuse = 0;//附加回蓝百分比
public float AutoMuse {
get {
return (float)getNature("autoMuse", autoMuse);
}
set {
autoMuse = (float)setNature("autoMuse", value);
}
}
[SerializeField]
float needGlod; // 生产需要金币
public float NeedGlod {
get {
return (float)getNature("needGlod", needGlod);
}
set {
needGlod = (float)setNature("needGlod", value);
}
}
[SerializeField]
float needWood; // 生产需要木材
public float NeedWood {
get {
return (float)getNature("needWood", needWood);
}
set {
needWood = (float)setNature("needWood", value);
}
}
[SerializeField]
float needFood; // 生产需要食物
public float NeedFood {
get {
return (float)getNature("needFood", needFood);
}
set {
needFood = (float)setNature("needFood", value);
}
}
//状态属性
[SerializeField, HeaderAttribute("------状态属性------"), Space(15)]
private Vector3 lastPos;
public int mode = 0;
public int team = 0;
public GameNpc enemyNpc;
public bool isDie = false;
public float speedTime = 0;
public bool isCanFight = false;
public bool isNpc = true;
public bool isCanDie = true;
public bool isAutoFullStaut = true;
// 特殊部件
public int x;
public int y;
public int z;
public Dictionary<string, System.Object> nature = new Dictionary<string, System.Object>();
// 内部属性
float time;
/************************
*不对外开放的函数
*
*
************************/
//初始化函数
void Start() {
this.Hp = MaxHp;
this.Mp = MaxMp;
Vector3 pos = this.transform.position;
pos.y += 1f;
}
//刷新函数
void FixedUpdate () {
if (Hp > 0) {
time += Time.fixedDeltaTime;
if (time >= 1.0f / 2.0f) {
time = 0.0f;
this.addHp(RecoverHpPerSecond);
this.addMp(RecoverMpPerSecond);
this.addHp(this.MaxHp * (AutoHeal / 100f) * Time.fixedDeltaTime);
this.addMp(this.MaxMp * (AutoMuse / 100f) * Time.fixedDeltaTime);
}
}
}
/************************
*对外开放的接口
*
*
************************/
public void save(string str) {
PlayerPrefs.SetInt(str + "_id", Id);
PlayerPrefs.SetInt(str + "_x", x);
PlayerPrefs.SetInt(str + "_y", y);
PlayerPrefs.SetInt(str + "_z", z);
PlayerPrefs.SetString(str + "_npcName", NpcName);
PlayerPrefs.SetFloat(str + "_hp", Hp);
PlayerPrefs.SetFloat(str + "_maxHp", MaxHp);
PlayerPrefs.SetFloat(str + "_mp", Mp);
PlayerPrefs.SetFloat(str + "_maxMp", MaxMp);
PlayerPrefs.SetFloat(str + "_pAttLow", PAttLow);
PlayerPrefs.SetFloat(str + "_pAttHigh", PAttHigh);
PlayerPrefs.SetFloat(str + "_mAttLow", MAttLow);
PlayerPrefs.SetFloat(str + "_mAttHigh", MAttHigh);
PlayerPrefs.SetFloat(str + "_pDefPower", PDefPower);
PlayerPrefs.SetFloat(str + "_mDefPower", MDefPower);
PlayerPrefs.SetInt(str + "_lvl", Lvl);
PlayerPrefs.SetInt(str + "_maxLvl", MaxLvl);
PlayerPrefs.SetFloat(str + "_exp", Exp);
PlayerPrefs.SetFloat(str + "_dropExpOdds", DropExpOdds);
PlayerPrefs.SetFloat(str + "_money", Money);
PlayerPrefs.SetFloat(str + "_maxMoney", MaxMoney);
PlayerPrefs.SetFloat(str + "_diamond", Diamond);
PlayerPrefs.SetFloat(str + "_maxDiamond", MaxDiamond);
PlayerPrefs.SetFloat(str + "_dropMoneyOdds", DropMoneyOdds);
PlayerPrefs.SetFloat(str + "_norFightSpeed", NorFightSpeed);
PlayerPrefs.SetFloat(str + "_fightSpeedAmp", FightSpeedAmp);
PlayerPrefs.SetFloat(str + "_pVioHurtOdds", PVioHurtOdds);
PlayerPrefs.SetFloat(str + "_pVioHurtAmp", PVioHurtAmp);
PlayerPrefs.SetFloat(str + "_pVioHurtAdd", PVioHurtAdd);
PlayerPrefs.SetFloat(str + "_pVioHurtMissDefOdds", PVioHurtMissDefOdds);
PlayerPrefs.SetFloat(str + "_pVioHurtMissDefAmp", PVioHurtMissDefAmp);
PlayerPrefs.SetFloat(str + "_pVioHurtMissDefAdd", PVioHurtMissDefAdd);
PlayerPrefs.SetFloat(str + "_doeOdds", DoeOdds);
PlayerPrefs.SetFloat(str + "_recoverHpPerSecond", RecoverHpPerSecond);
PlayerPrefs.SetFloat(str + "_recoverMpPerSecond", RecoverMpPerSecond);
PlayerPrefs.SetFloat(str + "_recoverHpPerHit", RecoverHpPerHit);
PlayerPrefs.SetFloat(str + "_recoverMpPerHit", RecoverMpPerHit);
PlayerPrefs.SetFloat(str + "_extraItemOdds", ExtraItemOdds);
PlayerPrefs.SetFloat(str + "_extraExpOdds", ExtraExpOdds);
PlayerPrefs.SetFloat(str + "_extraMoneyOdds", ExtraMoneyOdds);
PlayerPrefs.SetFloat(str + "_autoHeal", AutoHeal);
PlayerPrefs.SetFloat(str + "_autoMuse", AutoMuse);
PlayerPrefs.SetString(str + "_conText", ConText);
}
public void load(string str) {
if (PlayerPrefs.GetInt(str + "_id") == 0) {
save(str);
} else {
Id = PlayerPrefs.GetInt(str + "_id");
x = PlayerPrefs.GetInt(str + "_x");
y = PlayerPrefs.GetInt(str + "_y");
z = PlayerPrefs.GetInt(str + "_z");
NpcName = PlayerPrefs.GetString(str + "_npcName");
Hp = PlayerPrefs.GetFloat(str + "_hp");
MaxHp = PlayerPrefs.GetFloat(str + "_maxHp");
Mp = PlayerPrefs.GetFloat(str + "_mp");
MaxMp = PlayerPrefs.GetFloat(str + "_maxMp");
PAttLow = PlayerPrefs.GetFloat(str + "_pAttLow");
PAttHigh = PlayerPrefs.GetFloat(str + "_pAttHigh");
MAttLow = PlayerPrefs.GetFloat(str + "_mAttLow");
MAttHigh = PlayerPrefs.GetFloat(str + "_mAttHigh");
PDefPower = PlayerPrefs.GetFloat(str + "_pDefPower");
MDefPower = PlayerPrefs.GetFloat(str + "_mDefPower");
Lvl = PlayerPrefs.GetInt(str + "_lvl");
MaxLvl = PlayerPrefs.GetInt(str + "_maxLvl");
Exp = PlayerPrefs.GetFloat(str + "_exp");
DropExpOdds = PlayerPrefs.GetFloat(str + "_dropExpOdds");
Money = PlayerPrefs.GetFloat(str + "_money");
MaxMoney = PlayerPrefs.GetFloat(str + "_maxMoney");
Diamond = PlayerPrefs.GetFloat(str + "_diamond");
MaxDiamond = PlayerPrefs.GetFloat(str + "_maxDiamond");
DropMoneyOdds = PlayerPrefs.GetFloat(str + "_dropMoneyOdds");
NorFightSpeed = PlayerPrefs.GetFloat(str + "_norFightSpeed");
PVioHurtOdds = PlayerPrefs.GetFloat(str + "_pVioHurtOdds");
PVioHurtAmp = PlayerPrefs.GetFloat(str + "_pVioHurtAmp");
PVioHurtAdd = PlayerPrefs.GetFloat(str + "_pVioHurtAdd");
PVioHurtMissDefOdds = PlayerPrefs.GetFloat(str + "_pVioHurtMissDefOdds");
PVioHurtMissDefAmp = PlayerPrefs.GetFloat(str + "_pVioHurtMissDefAmp");
PVioHurtMissDefAdd = PlayerPrefs.GetFloat(str + "_pVioHurtMissDefAdd");
DoeOdds = PlayerPrefs.GetFloat(str + "_doeOdds");
RecoverHpPerSecond = PlayerPrefs.GetFloat(str + "_recoverHpPerSecond");
RecoverMpPerSecond = PlayerPrefs.GetFloat(str + "_recoverMpPerSecond");
RecoverHpPerHit = PlayerPrefs.GetFloat(str + "_recoverHpPerHit");
RecoverMpPerHit = PlayerPrefs.GetFloat(str + "_recoverMpPerHit");
ExtraItemOdds = PlayerPrefs.GetFloat(str + "_extraItemOdds");
ExtraExpOdds = PlayerPrefs.GetFloat(str + "_extraExpOdds");
ExtraMoneyOdds = PlayerPrefs.GetFloat(str + "_extraMoneyOdds");
AutoHeal = PlayerPrefs.GetFloat(str + "_autoHeal");
AutoMuse = PlayerPrefs.GetFloat(str + "_autoMuse");
ConText = PlayerPrefs.GetString(str + "_conText");
}
}
public void copyOf(GameNpc npc) {
Id = npc.Id;
NpcName = npc.NpcName;
Hp = npc.Hp;
MaxHp = npc.MaxHp;
Mp = npc.Mp;
MaxMp = npc.MaxMp;
PAttLow = npc.PAttLow;
PAttHigh = npc.PAttHigh;
MAttLow = npc.MAttLow;
MAttHigh = npc.MAttHigh;
PDefPower = npc.PDefPower;
MDefPower = npc.MDefPower;
Lvl = npc.Lvl;
MaxLvl = npc.MaxLvl;
Exp = npc.Exp;
DropExpOdds = npc.DropExpOdds;
Money = npc.Money;
MaxMoney = npc.MaxMoney;
Diamond = npc.Diamond;
MaxDiamond = npc.MaxDiamond;
DropMoneyOdds = npc.DropMoneyOdds;
NorFightSpeed = npc.NorFightSpeed;
PVioHurtOdds = npc.PVioHurtOdds;
PVioHurtAmp = npc.PVioHurtAmp;
PVioHurtAdd = npc.PVioHurtAdd;
PVioHurtMissDefOdds = npc.PVioHurtMissDefOdds;
PVioHurtMissDefAmp = npc.PVioHurtMissDefAmp;
PVioHurtMissDefAdd = npc.PVioHurtMissDefAdd;
DoeOdds = npc.DoeOdds;
RecoverHpPerSecond = npc.RecoverHpPerSecond;
RecoverMpPerSecond = npc.RecoverMpPerSecond;
RecoverHpPerHit = npc.RecoverHpPerHit;
RecoverMpPerHit = npc.RecoverMpPerHit;
ExtraItemOdds = npc.ExtraItemOdds;
ExtraExpOdds = npc.ExtraExpOdds;
ExtraMoneyOdds = npc.ExtraMoneyOdds;
AutoHeal = npc.AutoHeal;
AutoMuse = npc.AutoMuse;
ConText = npc.ConText;
x = npc.x;
y = npc.y;
z = npc.z;
}
//升级
public void upGrade(int l) {
Lvl += l;
MaxExp = Lvl * Lvl * 20;
this.MaxHp += 5 * l;
this.MaxMp += 3 * l;
this.Hp = MaxHp;
this.Mp = MaxMp;
this.PAttLow += 1 * l;
this.PAttHigh += 2 * l;
this.MAttLow += 0.6f * l;
this.MAttHigh += 1.2f * l;
this.PDefPower += 0.3f * l;
this.MDefPower += 0.15f * l;
}
//传送基于父亲
public void moveToPosOfParent(Vector3 v){
lastPos = this.transform.position;
this.transform.localPosition = v;
}
//传送
public void moveToPosOf(Vector3 v) {
lastPos = this.transform.position;
this.transform.position = v;
}
// 返回上一个传送点
public void backLastPos() {
this.transform.position = lastPos;
}
//一般攻击
public void normalAttack(GameNpc npc){
npc.minusHp(getPhysicsRealAttack(this,npc));
if (npc.Hp <= 0 && !npc.isDie) {
addExp(npc.Lvl * 2.5f + npc.Exp / 10);
addMoney(npc.Lvl + npc.Money);
Diamond += npc.Diamond;
PlayerPrefs.SetFloat("Diamond", Diamond);
}
}
//获得攻击
public static float getPhysicsRealAttack(GameNpc attNpc,GameNpc defNpc){
float pAtt = Random.Range (attNpc.PAttLow, attNpc.PAttHigh);
float pDef = defNpc.PDefPower;
if (Random.Range (1, 100) <= attNpc.PVioHurtOdds) {//暴击时
pAtt *= ((attNpc.PVioHurtAmp/100f)+1f);
pAtt += attNpc.PVioHurtAdd;
if (Random.Range (1, 100) >= attNpc.PVioHurtMissDefOdds) {
if (1 - attNpc.PVioHurtMissDefAmp / 100f <= 0) {
pDef = 0;
}
else {
pDef *= 1 - attNpc.PVioHurtMissDefAmp / 100f;
}
if (attNpc.PVioHurtMissDefAdd >= pDef) {
pDef = 0;
} else {
pDef -= attNpc.PVioHurtMissDefAdd;
}
}
}
pAtt -= pDef;
return pAtt;
}
public int getFightPower() {
float power = 0;
power += this.MaxHp;
power += this.MaxMp;
power += this.PAttLow*3;
power += this.PAttHigh*5;
power += this.MAttLow * 3;
power += this.MAttHigh * 5;
power += this.PDefPower * 4;
power += this.MDefPower * 4;
return (int)power;
}
//获得攻击
public static float getMagicRealAttack(GameNpc attNpc, GameNpc defNpc) {
float pAtt = Random.Range(attNpc.PAttLow, attNpc.PAttHigh);
float pDef = defNpc.PDefPower;
if (Random.Range(1, 100) <= attNpc.PVioHurtOdds) {//暴击时
pAtt *= ((attNpc.PVioHurtAmp / 100f) + 1f);
pAtt += attNpc.PVioHurtAdd;
if (Random.Range(1, 100) >= attNpc.PVioHurtMissDefOdds) {
if (1 - attNpc.PVioHurtMissDefAmp / 100f <= 0) {
pDef = 0;
} else {
pDef *= 1 - attNpc.PVioHurtMissDefAmp / 100f;
}
if (attNpc.PVioHurtMissDefAdd >= pDef) {
pDef = 0;
} else {
pDef -= attNpc.PVioHurtMissDefAdd;
}
}
}
pAtt -= pDef;
return pAtt;
}
/************************
* 封装函数
*
*
************************/
public System.Object getNature(string key, System.Object obj) {
if (!nature.ContainsKey("key")) {
nature.Add("key", obj);
}
return nature["key"];
}
public System.Object setNature(string key, System.Object obj) {
if (!nature.ContainsKey("key")) {
nature.Add("key", obj);
}
nature["key"] = obj;
return nature["key"];
}
//增加经验
public int addExp(float aExp) {
this.Exp += aExp;
int n = 0;
while (this.Lvl < this.MaxLvl && this.Exp >= MaxExp) {
this.Exp -= MaxExp;
upGrade(1);
n++;
}
if (this.Exp >= this.MaxExp * 10f) {
this.Exp = this.MaxExp * 10f;
}
return n;
}
//减少经验
public bool deleteExp(float dExp) {
this.Exp -= dExp;
if (this.Exp < 0) {
this.Exp = 0;
return false;
}
return true;
}
//增加金钱
public int addMoney(float aMoney) {
this.Money += aMoney;
int n = 0;
if (this.Money >= this.MaxMoney * 10f) {
this.Money = this.MaxMoney * 10f;
}
return n;
}
//减少金钱
public void deleteMoney(float dMoney) {
this.Money -= dMoney;
if (this.Money < 0) {
this.Money = 0;
}
}
public void addHp(float hp) {
this.Hp += hp;
if (this.Hp >= this.MaxHp) {
this.Hp = this.MaxHp;
}
}
public void minusHp(float hp) {
this.Hp -= hp;
if (this.Hp < 0) {
this.Hp = 0;
}
}
public void addMp(float mp) {
this.Mp += Hp;
if (this.Mp >= this.MaxMp) {
this.Mp = this.MaxMp;
}
}
public void minusMp(float mp) {
this.Mp -= mp;
if (this.Mp < 0) {
this.Mp = 0;
}
}
public string toString() {
string str;
str = NpcName + "[Lv." + Lvl + "/" + MaxLvl + "]\n------\n";
str += "Hp:" + Hp.ToString("F2") + "/" + MaxHp.ToString("F2") + "\n";
str += "Mp:" + Mp.ToString("F2") + "/" + MaxMp.ToString("F2") + "\n";
str += "攻击力:" + PAttLow.ToString("F2") + "-" + PAttLow.ToString("F2") + "\n";
str += "防御力:" + PAttLow.ToString("F2") + "-" + PAttLow.ToString("F2") + "\n";
return str;
}
}
建筑
using UnityEngine;
using System.Collections;
/* **************************
* 注意,继承时
* 除非必要一定要执行一遍base相应的函数
*
* ***************************/
public class Build : GameNpc {
protected void Awake() {
base.Awake();
type = Active.TYPE_BUILD;
}
[SerializeField]
float buildTime = 10.0f; // 建造时间(几秒建完)
public float BuildTime {
get {
return (float)getNature("buildTime", buildTime);
}
set {
buildTime = (float)setNature("buildTime", value);
}
}
[SerializeField]
float hasBuildTime = 0.0f; // 已经建造时间
public float HasBuildTime {
get {
return (float)getNature("hasBuildTime", hasBuildTime);
}
set {
hasBuildTime = (float)setNature("hasBuildTime", value);
}
}
[SerializeField]
float repairTime = 5.0f; // 修理速率(修理即回血, 几秒能修理完)
public float RepairTime {
get {
return (float)getNature("repairTime", repairTime);
}
set {
repairTime = (float)setNature("repairTime", value);
}
}
[SerializeField]
float repairNeedOdds; // 修复花费比例(金钱和木头)
public float RepairNeedOdds {
get {
return (float)getNature("repairNeedOdds", repairNeedOdds);
}
set {
repairNeedOdds = (float)setNature("repairNeedOdds", value);
}
}
// 是否可建造(满足建筑条件)
public virtual bool isCanBuild(params System.Object[] objs) {
return true;
}
}
人物
直接套用GameNpc即可,后期还要加点略微的改动,再有了物品和装备以后,不过呢,今天先到这,累死了- -。
估计今天人形大宝宝要赖床了。。。