AI 加速和存档功能
This commit is contained in:
parent
111f0411a4
commit
4342355ea5
77
My project/Assets/Scripts/Data/GameRecordData.cs
Normal file
77
My project/Assets/Scripts/Data/GameRecordData.cs
Normal file
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* @Author: 白哉
|
||||
* @Description: 游戏记录
|
||||
* @Date: 2025年05月22日 星期四 11:05:24
|
||||
* @Modify:
|
||||
*/
|
||||
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Logic.AI;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
namespace RuntimeData
|
||||
{
|
||||
public class GameRecordManager
|
||||
{
|
||||
public static GameRecordManager Instance = new GameRecordManager();
|
||||
private GameRecordData _gameRecord;
|
||||
public GameRecordData GameRecordData
|
||||
{
|
||||
get
|
||||
{
|
||||
RefreshGameRecord();
|
||||
return _gameRecord;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void AddRecord(GameRecord record)
|
||||
{
|
||||
RefreshGameRecord();
|
||||
_gameRecord.Records.Add(record);
|
||||
string json = JsonUtility.ToJson(_gameRecord);
|
||||
File.WriteAllText(Application.persistentDataPath + "/game_record.json", json);
|
||||
}
|
||||
|
||||
public void RefreshGameRecord()
|
||||
{
|
||||
if (_gameRecord != null) return;
|
||||
string path = Application.persistentDataPath + "/game_record.json";
|
||||
if (File.Exists(path))
|
||||
{
|
||||
string json = File.ReadAllText(path);
|
||||
_gameRecord = JsonUtility.FromJson<GameRecordData>(json);
|
||||
}
|
||||
if (_gameRecord == null) _gameRecord = new GameRecordData();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[System.Serializable]
|
||||
public class GameRecordData
|
||||
{
|
||||
public List<GameRecord> Records;
|
||||
|
||||
|
||||
public GameRecordData()
|
||||
{
|
||||
Records = new List<GameRecord>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[System.Serializable]
|
||||
public class GameRecord
|
||||
{
|
||||
public float Score;
|
||||
public string Time;
|
||||
public uint CityCount;
|
||||
public uint MapWidth;
|
||||
public uint MapHeight;
|
||||
public uint PlayerCount;
|
||||
public AIDifficult AIDiff;
|
||||
}
|
||||
}
|
||||
3
My project/Assets/Scripts/Data/GameRecordData.cs.meta
Normal file
3
My project/Assets/Scripts/Data/GameRecordData.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a69a0a44453d41b6a639533b92c19b99
|
||||
timeCreated: 1747884499
|
||||
@ -10,6 +10,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Logic.Action;
|
||||
using Logic.AI;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
@ -22,14 +23,16 @@ namespace RuntimeData
|
||||
public uint Width;
|
||||
public uint Height;
|
||||
public uint PlayerCount;
|
||||
public AIDifficult AIDiff;
|
||||
|
||||
public uint selfCivId;
|
||||
public uint selfForceId;
|
||||
public MapConfig(uint width, uint height, uint playerCount,uint civId, uint forceId)
|
||||
public MapConfig(uint width, uint height, uint playerCount,uint civId, uint forceId, AIDifficult aiDiff = AIDifficult.Normal)
|
||||
{
|
||||
Width = width;
|
||||
Height = height;
|
||||
PlayerCount = playerCount;
|
||||
AIDiff = aiDiff;
|
||||
selfCivId = civId;
|
||||
selfForceId = forceId;
|
||||
}
|
||||
@ -838,6 +841,22 @@ namespace RuntimeData
|
||||
MapData map = JsonUtility.FromJson<MapData>(json);
|
||||
return map;
|
||||
}
|
||||
|
||||
public GameRecord ExportGameRecord()
|
||||
{
|
||||
var gameRecord = new GameRecord();
|
||||
gameRecord.Score = 0;
|
||||
DateTime now = DateTime.Now;
|
||||
gameRecord.Time = now.ToString("yyyy-MM-dd HH-mm-ss");
|
||||
var cityList = new List<CityData>();
|
||||
GetCityDataListByPlayerId(PlayerMap.SelfPlayerId, cityList);
|
||||
gameRecord.CityCount = (uint)cityList.Count;
|
||||
gameRecord.MapWidth = MapConfig.Width;
|
||||
gameRecord.MapHeight = MapConfig.Height;
|
||||
gameRecord.PlayerCount = MapConfig.PlayerCount - 1;
|
||||
gameRecord.AIDiff = MapConfig.AIDiff;
|
||||
return gameRecord;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -11,16 +11,42 @@ using Logic.Action;
|
||||
|
||||
namespace Logic.AI
|
||||
{
|
||||
public enum AIDifficult
|
||||
{
|
||||
Simple,
|
||||
Normal,
|
||||
Hard,
|
||||
}
|
||||
|
||||
|
||||
public class AIActionBase
|
||||
{
|
||||
public CalculateResult Result;
|
||||
public CommonActionParams Param;
|
||||
public ActionLogicBase ActionLogic;
|
||||
public bool IsInSight;
|
||||
|
||||
|
||||
public AIActionBase(CommonActionParams param, ActionLogicBase action)
|
||||
{
|
||||
Param = param;
|
||||
ActionLogic = action;
|
||||
IsInSight = false;
|
||||
}
|
||||
|
||||
public void CheckIsActionInPlayerSight()
|
||||
{
|
||||
IsInSight = true;
|
||||
var player = Param.MapData.PlayerMap.SelfPlayerData;
|
||||
if (Param.MapData.GridMap.GetGridDataByGid(Param.GridId, out var grid)
|
||||
&& player.Sight.CheckIsInSight(grid.Id)) return;
|
||||
if (Param.MapData.GetGridDataByUnitId(Param.UnitId, out var unitGrid)
|
||||
&& player.Sight.CheckIsInSight(unitGrid.Id)) return;
|
||||
if (Param.MapData.GetGridDataByUnitId(Param.TargetUnitId, out var targetUnit)
|
||||
&& player.Sight.CheckIsInSight(targetUnit.Id)) return;
|
||||
if (Param.MapData.GetGridDataByCityId(Param.CityId, out var cityGrid)
|
||||
&& player.Sight.CheckIsInSight(cityGrid.Id)) return;
|
||||
IsInSight = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -127,11 +127,13 @@ namespace Logic.AI
|
||||
$"位置{grid.Pos.X}, {grid.Pos.Y}, " +
|
||||
$"目标 {MaxScoreAction.Param.GridData.Pos.X}, {MaxScoreAction.Param.GridData.Pos.Y}");
|
||||
}
|
||||
|
||||
|
||||
MaxScoreAction.CheckIsActionInPlayerSight();
|
||||
MaxScoreAction.ActionLogic.Execute(MaxScoreAction.Param);
|
||||
MaxScoreAction = null;
|
||||
AILogicState = AILogicState.Pausing;
|
||||
_recordTime = Time.time;
|
||||
if (!MaxScoreAction.IsInSight) _recordTime -= DebugCenter.Instance.DebugAIActionTime - 0.04f;
|
||||
MaxScoreAction = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -252,6 +252,9 @@ namespace Logic
|
||||
{
|
||||
Turn++;
|
||||
PlayerPrefs.SetInt("Archive", 0);
|
||||
|
||||
var record = _gameLogic.Main.MapData.ExportGameRecord();
|
||||
GameRecordManager.Instance.AddRecord(record);
|
||||
}
|
||||
|
||||
public override void End()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user