Made the Game.Enter event only fire once loading finishes and fixed player building mode

Also attempted to fix material changing and updating the rendered block
This commit is contained in:
Norbi Peti 2021-11-25 01:48:06 +01:00
parent f53d0b63e7
commit e3a7961be4
6 changed files with 76 additions and 21 deletions

View file

@ -7,17 +7,17 @@ using Svelto.ECS;
using Svelto.Tasks;
using Svelto.Tasks.Lean;
using RobocraftX.Blocks;
using RobocraftX.Common.Loading;
using RobocraftX.ScreenshotTaker;
using Techblox.Environment.Transition;
using Techblox.GameSelection;
using TechbloxModdingAPI.Blocks;
using TechbloxModdingAPI.Engines;
using TechbloxModdingAPI.Tasks;
using TechbloxModdingAPI.Utility;
namespace TechbloxModdingAPI.App
{
public class GameGameEngine : IApiEngine
public class GameGameEngine : IApiEngine, IReactOnAddAndRemove<LoadingActionEntityStruct>
{
public WrappedHandler<GameEventArgs> EnterGame;
@ -29,6 +29,8 @@ namespace TechbloxModdingAPI.App
public EntitiesDB entitiesDB { set; private get; }
private bool enteredGame;
public void Dispose()
{
ExitGame.Invoke(this, new GameEventArgs { GameName = GetGameData().saveName, GamePath = GetGameData().gameID });
@ -37,14 +39,7 @@ namespace TechbloxModdingAPI.App
public void Ready()
{
EnteringGame().RunOn(Scheduler.leanRunner);
}
private IEnumerator<TaskContract> EnteringGame()
{
yield return new WaitForSubmissionEnumerator(GameLoadedEnginePatch.Scheduler).Continue();
EnterGame.Invoke(this, new GameEventArgs { GameName = GetGameData().saveName, GamePath = GetGameData().gameID });
IsInGame = true;
enteredGame = true;
}
// game functionality
@ -139,5 +134,17 @@ namespace TechbloxModdingAPI.App
{
return entitiesDB.QueryEntity<GameSelectionComponent>(GameSelectionConstants.GameSelectionEGID);
}
}
public void Add(ref LoadingActionEntityStruct entityComponent, EGID egid)
{
}
public void Remove(ref LoadingActionEntityStruct entityComponent, EGID egid)
{ // Finished loading
if (!enteredGame) return;
EnterGame.Invoke(this, new GameEventArgs { GameName = GetGameData().saveName, GamePath = GetGameData().gameID });
IsInGame = true;
enteredGame = false;
}
}
}

View file

@ -327,7 +327,10 @@ namespace TechbloxModdingAPI
: throw new BlockTypeException("Unknown block type! Could not set default material.");
if (!FullGameFields._dataDb.ContainsKey<MaterialPropertiesData>(val))
throw new BlockException($"Block material {value} does not exist!");
BlockEngine.GetBlockInfo<CubeMaterialStruct>(this).materialId = val;
ref var comp = ref BlockEngine.GetBlockInfo<CubeMaterialStruct>(this);
if (comp.materialId == val)
return;
comp.materialId = val;
BlockEngine.UpdatePrefab(this, val, Flipped); //The default causes the screen to go black
}
}

View file

@ -5,7 +5,9 @@ using System.Reflection;
using DataLoader;
using Svelto.Tasks;
using Svelto.Tasks.Enumerators;
using Unity.Mathematics;
using UnityEngine;
using TechbloxModdingAPI.App;
using TechbloxModdingAPI.Tests;
@ -86,6 +88,12 @@ namespace TechbloxModdingAPI.Blocks
foreach (var property in block.GetType().GetProperties())
{
if (property.Name == "Material" || property.Name == "Flipped") continue; // TODO: Crashes in game
if (property.Name == "Material" || property.Name == "Flipped")
{
Console.WriteLine("Block type: "+block.Type);
Console.WriteLine("Will set " + property.Name);
yield return new WaitForSecondsEnumerator(1).Continue();
}
//Includes specialised block properties
if (property.SetMethod == null) continue;
var testValues = new (Type, object, Predicate<object>)[]

View file

@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using HarmonyLib;
using Gamecraft.ColourPalette;
using Gamecraft.TimeRunning;
@ -11,8 +13,8 @@ using RobocraftX.Rendering;
using RobocraftX.Rendering.GPUI;
using Svelto.DataStructures;
using Svelto.ECS;
using Svelto.ECS.EntityStructs;
using Svelto.ECS.Hybrid;
using Techblox.BuildingDrone;
using Unity.Mathematics;
using TechbloxModdingAPI.Engines;
@ -92,10 +94,7 @@ namespace TechbloxModdingAPI.Blocks.Engines
public void UpdateDisplayedBlock(EGID id)
{
if (!BlockExists(id)) return;
var pos = entitiesDB.QueryEntity<PositionEntityStruct>(id);
var rot = entitiesDB.QueryEntity<RotationEntityStruct>(id);
var scale = entitiesDB.QueryEntity<ScalingEntityStruct>(id);
entitiesDB.QueryEntity<RenderingDataStruct>(id).matrix = float4x4.TRS(pos.position, rot.rotation, scale.scale);
RenderingPatch.UpdateBlocks();
}
internal void UpdatePrefab(Block block, byte material, bool flipped)
@ -115,7 +114,17 @@ namespace TechbloxModdingAPI.Blocks.Engines
PrefabsID.GetOrCreatePrefabID((ushort) prefabAssetID, material, 1, flipped);
entitiesDB.QueryEntityOrDefault<GFXPrefabEntityStructGPUI>(block).prefabID = prefabId;
if (block.Exists)
{
entitiesDB.PublishEntityChange<CubeMaterialStruct>(block.Id);
entitiesDB.PublishEntityChange<GFXPrefabEntityStructGPUI>(block.Id);
ref BuildingActionComponent local =
ref entitiesDB.QueryEntity<BuildingActionComponent>(BuildingDroneUtility
.GetLocalBuildingDrone(entitiesDB).ToEGID(entitiesDB));
local.buildAction = BuildAction.ChangeMaterial;
local.targetPosition = block.Position;
this.entitiesDB.PublishEntityChange<BuildingActionComponent>(local.ID);
}
//Phyiscs prefab: prefabAssetID, set on block creation from the CubeListData
}
@ -239,5 +248,27 @@ namespace TechbloxModdingAPI.Blocks.Engines
return entitiesDB;
}
#endif
[HarmonyPatch]
public static class RenderingPatch
{
private static ComputeRenderingEntitiesMatricesEngine Engine;
public static void Postfix(ComputeRenderingEntitiesMatricesEngine __instance)
{
Engine = __instance;
}
public static MethodBase TargetMethod()
{
return typeof(ComputeRenderingEntitiesMatricesEngine).GetConstructors()[0];
}
public static void UpdateBlocks()
{
var data = new RenderingDataStruct();
Engine.Add(ref data, new EGID(0, CommonExclusiveGroups.BUTTON_BLOCK_GROUP));
}
}
}
}

View file

@ -360,8 +360,8 @@ namespace TechbloxModdingAPI
/// <summary>
/// The player's mode in time stopped mode, determining what they place.
/// </summary>
public PlayerBuildingMode BuildingMode => (PlayerBuildingMode) playerEngine
.GetCharacterStruct<TimeStoppedModeComponent>(Id).Get().timeStoppedContext;
public PlayerBuildingMode BuildingMode => (PlayerBuildingMode)Math.Log((double)playerEngine
.GetCharacterStruct<TimeStoppedModeComponent>(Id).Get().timeStoppedContext, 2); // It's a bit field in game now
/// <summary>
/// Whether the player is sprinting.
@ -522,6 +522,11 @@ namespace TechbloxModdingAPI
return (int) Id;
}
public override string ToString()
{
return $"{nameof(Type)}: {Type}, {nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Rotation)}: {Rotation}, {nameof(Mass)}: {Mass}";
}
// internal methods
internal static void Init()

View file

@ -6,6 +6,7 @@
ColourMode,
ConfigMode,
BlueprintMode,
MaterialMode
MaterialMode,
LandscapeMode
}
}