初始化仓库
This commit is contained in:
@@ -0,0 +1,248 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using Definition.DataStruct;
|
||||
using Definition.Enum;
|
||||
using Entity;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace StarForce
|
||||
{
|
||||
/// <summary>
|
||||
/// AI 工具类。
|
||||
/// </summary>
|
||||
public static class AIUtility
|
||||
{
|
||||
private static Dictionary<CampPair, RelationType> s_CampPairToRelation =
|
||||
new Dictionary<CampPair, RelationType>();
|
||||
|
||||
private static Dictionary<KeyValuePair<CampType, RelationType>, CampType[]> s_CampAndRelationToCamps =
|
||||
new Dictionary<KeyValuePair<CampType, RelationType>, CampType[]>();
|
||||
|
||||
static AIUtility()
|
||||
{
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Player, CampType.Player), RelationType.Friendly);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Player, CampType.Enemy), RelationType.Hostile);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Player, CampType.Neutral), RelationType.Neutral);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Player, CampType.Player2), RelationType.Hostile);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Player, CampType.Enemy2), RelationType.Hostile);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Player, CampType.Neutral2), RelationType.Neutral);
|
||||
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Enemy, CampType.Enemy), RelationType.Friendly);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Enemy, CampType.Neutral), RelationType.Neutral);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Enemy, CampType.Player2), RelationType.Hostile);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Enemy, CampType.Enemy2), RelationType.Hostile);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Enemy, CampType.Neutral2), RelationType.Neutral);
|
||||
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Neutral, CampType.Neutral), RelationType.Neutral);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Neutral, CampType.Player2), RelationType.Neutral);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Neutral, CampType.Enemy2), RelationType.Neutral);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Neutral, CampType.Neutral2), RelationType.Hostile);
|
||||
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Player2, CampType.Player2), RelationType.Friendly);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Player2, CampType.Enemy2), RelationType.Hostile);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Player2, CampType.Neutral2), RelationType.Neutral);
|
||||
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Enemy2, CampType.Enemy2), RelationType.Friendly);
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Enemy2, CampType.Neutral2), RelationType.Neutral);
|
||||
|
||||
s_CampPairToRelation.Add(new CampPair(CampType.Neutral2, CampType.Neutral2), RelationType.Neutral);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取两个阵营之间的关系。
|
||||
/// </summary>
|
||||
/// <param name="first">阵营一。</param>
|
||||
/// <param name="second">阵营二。</param>
|
||||
/// <returns>阵营间关系。</returns>
|
||||
public static RelationType GetRelation(CampType first, CampType second)
|
||||
{
|
||||
if (first > second)
|
||||
{
|
||||
CampType temp = first;
|
||||
first = second;
|
||||
second = temp;
|
||||
}
|
||||
|
||||
RelationType relationType;
|
||||
if (s_CampPairToRelation.TryGetValue(new CampPair(first, second), out relationType))
|
||||
{
|
||||
return relationType;
|
||||
}
|
||||
|
||||
Log.Warning("Unknown relation between '{0}' and '{1}'.", first.ToString(), second.ToString());
|
||||
return RelationType.Unknown;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取和指定具有特定关系的所有阵营。
|
||||
/// </summary>
|
||||
/// <param name="camp">指定阵营。</param>
|
||||
/// <param name="relation">关系。</param>
|
||||
/// <returns>满足条件的阵营数组。</returns>
|
||||
public static CampType[] GetCamps(CampType camp, RelationType relation)
|
||||
{
|
||||
KeyValuePair<CampType, RelationType> key = new KeyValuePair<CampType, RelationType>(camp, relation);
|
||||
CampType[] result = null;
|
||||
if (s_CampAndRelationToCamps.TryGetValue(key, out result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
// TODO: GC Alloc
|
||||
List<CampType> camps = new List<CampType>();
|
||||
Array campTypes = Enum.GetValues(typeof(CampType));
|
||||
for (int i = 0; i < campTypes.Length; i++)
|
||||
{
|
||||
CampType campType = (CampType)campTypes.GetValue(i);
|
||||
if (GetRelation(camp, campType) == relation)
|
||||
{
|
||||
camps.Add(campType);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: GC Alloc
|
||||
result = camps.ToArray();
|
||||
s_CampAndRelationToCamps[key] = result;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取实体间的距离。
|
||||
/// </summary>
|
||||
/// <returns>实体间的距离。</returns>
|
||||
public static float GetDistance(EntityBase fromEntity, EntityBase toEntity)
|
||||
{
|
||||
Transform fromTransform = fromEntity.CachedTransform;
|
||||
Transform toTransform = toEntity.CachedTransform;
|
||||
return (toTransform.position - fromTransform.position).magnitude;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取实体间的平方距离。
|
||||
/// </summary>
|
||||
/// <returns>实体间的平方距离。</returns>
|
||||
public static float GetSqrMagnitude(EntityBase fromEntity, EntityBase toEntity)
|
||||
{
|
||||
Transform fromTransform = fromEntity.CachedTransform;
|
||||
Transform toTransform = toEntity.CachedTransform;
|
||||
return (toTransform.position - fromTransform.position).sqrMagnitude;
|
||||
}
|
||||
|
||||
public static void PerformCollision(TargetableObject entity, EntityBase other)
|
||||
{
|
||||
if (entity == null || other == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// TargetableObject target = other as TargetableObject;
|
||||
// if (target != null)
|
||||
// {
|
||||
// ImpactData entityImpactData = entity.GetImpactData();
|
||||
// ImpactData targetImpactData = target.GetImpactData();
|
||||
// if (GetRelation(entityImpactData.Camp, targetImpactData.Camp) == RelationType.Friendly)
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// int entityDamageHP = CalcDamageHP(targetImpactData.Attack, entityImpactData.Defense);
|
||||
// int targetDamageHP = CalcDamageHP(entityImpactData.Attack, targetImpactData.Defense);
|
||||
//
|
||||
// entity.ApplyDamage(target, entityDamageHP);
|
||||
// target.ApplyDamage(entity, targetDamageHP);
|
||||
// return;
|
||||
// }
|
||||
|
||||
Bullet bullet = other as Bullet;
|
||||
if (bullet != null)
|
||||
{
|
||||
ImpactData entityImpactData = entity.GetImpactData();
|
||||
ImpactData bulletImpactData = bullet.GetImpactData();
|
||||
if (GetRelation(entityImpactData.Camp, bulletImpactData.Camp) == RelationType.Friendly)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int entityDamageHP = CalcDamageHP(bulletImpactData.AttackBase, bulletImpactData.AttackStat,
|
||||
entityImpactData.DefenseStat, entityImpactData.DodgeStat);
|
||||
|
||||
entity.ApplyDamage(bullet, entityDamageHP);
|
||||
GameEntry.Entity.HideEntity(bullet);
|
||||
return;
|
||||
}
|
||||
|
||||
WeaponBase weapon = other as WeaponBase;
|
||||
if (weapon != null)
|
||||
{
|
||||
if (!weapon.IsAttacking) return;
|
||||
ImpactData entityImpactData = entity.GetImpactData();
|
||||
ImpactData weaponImpactData = weapon.GetImpactData();
|
||||
if (GetRelation(entityImpactData.Camp, weaponImpactData.Camp) == RelationType.Friendly)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int entityDamageHP = CalcDamageHP(weaponImpactData.AttackBase, weaponImpactData.AttackStat,
|
||||
entityImpactData.DefenseStat, entityImpactData.DefenseStat);
|
||||
|
||||
entity.ApplyDamage(weapon, entityDamageHP);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private static int CalcDamageHP(int attack, StatProperty attackStat, StatProperty defenseStat,
|
||||
StatProperty dodgeStat)
|
||||
{
|
||||
// 1. 处理闪避(闪避率取值 (0, 0.9),不允许拉满闪避)
|
||||
if (dodgeStat != null)
|
||||
{
|
||||
if (Random.value < Mathf.Clamp(dodgeStat.Percent, 0, 0.9f)) return 0;
|
||||
}
|
||||
|
||||
// 2. 处理攻击加成 最终伤害 = (基础伤害 + 伤害提升固定值) * 伤害提升率
|
||||
float damage = attack;
|
||||
if (attackStat != null)
|
||||
{
|
||||
damage = (attack + attackStat.Value) * attackStat.Percent;
|
||||
}
|
||||
|
||||
// 3. 处理防御加成 最终伤害 = (基础伤害 - 防御提升固定值) / 防御提升率
|
||||
if (defenseStat != null)
|
||||
{
|
||||
damage = (damage - defenseStat.Value) / defenseStat.Percent;
|
||||
}
|
||||
|
||||
// 将伤害限制为正整数,避免堆防御导致无法造成伤害
|
||||
if (damage < 1) return 1;
|
||||
else return Mathf.CeilToInt(damage);
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
private struct CampPair
|
||||
{
|
||||
private readonly CampType m_First;
|
||||
private readonly CampType m_Second;
|
||||
|
||||
public CampPair(CampType first, CampType second)
|
||||
{
|
||||
m_First = first;
|
||||
m_Second = second;
|
||||
}
|
||||
|
||||
public CampType First => m_First;
|
||||
|
||||
public CampType Second => m_Second;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f1870ca304d9c4d49905b5d3da2841e5
|
||||
timeCreated: 1528026163
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,68 @@
|
||||
using Text = GameFramework.Utility.Text;
|
||||
|
||||
namespace Game.Utility
|
||||
{
|
||||
public static class AssetUtility
|
||||
{
|
||||
public static string GetConfigAsset(string assetName, bool fromBytes)
|
||||
{
|
||||
return Text.Format("Assets/GameMain/Configs/{0}.{1}", assetName, fromBytes ? "bytes" : "txt");
|
||||
}
|
||||
|
||||
public static string GetDataTableAsset(string assetName, bool fromBytes)
|
||||
{
|
||||
return Text.Format("Assets/GameMain/DataTables/{0}.{1}", assetName, fromBytes ? "bytes" : "txt");
|
||||
}
|
||||
|
||||
public static string GetDictionaryAsset(string assetName, bool fromBytes)
|
||||
{
|
||||
return Text.Format("Assets/GameMain/Localization/{0}/Dictionaries/{1}.{2}",
|
||||
GameEntry.Localization.Language, assetName, fromBytes ? "bytes" : "xml");
|
||||
}
|
||||
|
||||
public static string GetFontAsset(string assetName)
|
||||
{
|
||||
return Text.Format("Assets/GameMain/Fonts/{0}.ttf", assetName);
|
||||
}
|
||||
|
||||
public static string GetTMPFontAsset(string assetName)
|
||||
{
|
||||
return Text.Format("Assets/GameMain/Fonts/{0}.asset", assetName);
|
||||
}
|
||||
|
||||
public static string GetSceneAsset(string assetName)
|
||||
{
|
||||
return Text.Format("Assets/GameMain/Scenes/{0}.unity", assetName);
|
||||
}
|
||||
|
||||
public static string GetMusicAsset(string assetName)
|
||||
{
|
||||
return Text.Format("Assets/GameMain/Music/{0}.mp3", assetName);
|
||||
}
|
||||
|
||||
public static string GetSoundAsset(string assetName)
|
||||
{
|
||||
return Text.Format("Assets/GameMain/Sounds/{0}.wav", assetName);
|
||||
}
|
||||
|
||||
public static string GetEntityAsset(string assetName)
|
||||
{
|
||||
return Text.Format("Assets/GameMain/Entities/{0}.prefab", assetName);
|
||||
}
|
||||
|
||||
public static string GetUIFormAsset(string assetName)
|
||||
{
|
||||
return Text.Format("Assets/GameMain/UI/UIForms/{0}.prefab", assetName);
|
||||
}
|
||||
|
||||
public static string GetUISoundAsset(string assetName)
|
||||
{
|
||||
return Text.Format("Assets/GameMain/UI/UISounds/{0}.wav", assetName);
|
||||
}
|
||||
|
||||
public static string GetUITextureIconAsset(string assetName)
|
||||
{
|
||||
return Text.Format("Assets/GameMain/UI/UISprites/Icons/{0}.png", assetName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ec33154bafd23a84a8f6a414f5b25ae2
|
||||
timeCreated: 1528026163
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace StarForce
|
||||
{
|
||||
public static class EnumUtility<T> where T : struct, System.Enum
|
||||
{
|
||||
private static readonly Dictionary<string, T> _enumCache = new();
|
||||
|
||||
public static T Get(string value)
|
||||
{
|
||||
if (!_enumCache.TryGetValue(value, out T result))
|
||||
{
|
||||
if (System.Enum.TryParse(value, true, out result))
|
||||
{
|
||||
_enumCache[value] = result;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Error($"Enum 解析失败:类型:{typeof(T).Name} 不包含值 {value}");
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a39ac998e79e504fa643b459880ee9e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Game.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// Newtonsoft.Json 函数集辅助器。
|
||||
/// </summary>
|
||||
public class JsonNetUtility : GameFramework.Utility.Json.IJsonHelper
|
||||
{
|
||||
public string ToJson(object obj)
|
||||
{
|
||||
return JsonConvert.SerializeObject(obj);
|
||||
}
|
||||
|
||||
public T ToObject<T>(string json)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
|
||||
public object ToObject(Type objectType, string json)
|
||||
{
|
||||
return JsonConvert.DeserializeObject(json, objectType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a2eb0a4b148e1c24ebc91cb3a2ef3180
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,24 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
|
||||
namespace StarForce
|
||||
{
|
||||
public static class WebUtility
|
||||
{
|
||||
public static string EscapeString(string stringToEscape)
|
||||
{
|
||||
return Uri.EscapeDataString(stringToEscape);
|
||||
}
|
||||
|
||||
public static string UnescapeString(string stringToUnescape)
|
||||
{
|
||||
return Uri.UnescapeDataString(stringToUnescape);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bbf9721704b3fd240b878d887bf58800
|
||||
timeCreated: 1528026163
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user