wpf游戏引擎的Components的entity组件实现

1.GameEntity.cs

using PrimalEditor.Common;

using PrimalEditor.GameProject;

using PrimalEditor.Utilities;

using System.Collections.ObjectModel;

using System.Diagnostics;

using System.Runtime.Serialization;

using System.Windows.Input;

namespace PrimalEditor.Components

{

DataContract

KnownType(typeof(Transform))

KnownType(typeof(Script))

class GameEntity : ViewModelBase

{

private bool _isActive;

DataMember

public bool IsActive

{

get => _isActive;

set

{

if (_isActive != value)

{

_isActive = value;

OnPropertyChanged(nameof(IsActive));

}

}

}

private bool _isEnabled;

DataMember

public bool IsEnabled

{

get => _isEnabled;

set

{

if (_isEnabled != value)

{

_isEnabled = value;

OnPropertyChanged(nameof(IsEnabled));

}

}

}

private string _name;

DataMember

public string Name

{

get => _name;

set

{

if (_name != value)

{

_name = value;

OnPropertyChanged(nameof(Name));

}

}

}

DataMember

public Scene ParentScene { get; private set; }

DataMember(Name = nameof(Components))

private readonly ObservableCollection<Component> _components = new ObservableCollection<Component>();

public ReadOnlyObservableCollection<Component> Components { get; private set; }

public Component GetComponent(Type type) => Components.FirstOrDefault(c => c.GetType() == type);

public T GetComponent<T>() where T : Component => GetComponent(typeof(T)) as T;

public bool AddComponent(Component component)

{

Debug.Assert(component != null);

if (!Components.Any(x => x.GetType() == component.GetType()))

{

IsActive = false;

_components.Add(component);

IsActive = true;

return true;

}

Logger.Log(MessageType.Warning, $"Entity {Name} already has a {component.GetType().Name} component");

return false;

}

public void RemoveComponent(Component componnet)

{

Debug.Assert(componnet != null);

if (componnet is Transform) return;

if (_components.Contains(componnet))

{

IsActive = false;

_components.Remove(componnet);

IsActive = true;

}

}

public ICommand RenameCommand { get; private set; }

public ICommand IsEnableCommand { get; private set; }

public object? DataContext { get; internal set; }

public GameEntity EntityId { get; internal set; }

OnDeserialized

void OnDeserialized(StreamingContext context)

{

if (_components != null)

{

Components = new ReadOnlyObservableCollection<Component>(_components);

OnPropertyChanged(nameof(Components));

}

RenameCommand = new RelayCommand<string>(x =>

{

var oldName = _name;

Name = x;

Project.UndoRedo.Add(new UndoRedoAction(nameof(Name), this,

oldName, x, $"Rename entity '{oldName}' to '{x}'"

));

}, x => x != _name

);

IsEnableCommand = new RelayCommand<bool>(x =>

{

var oldValue = _isEnabled;

IsEnabled = x;

Project.UndoRedo.Add(new UndoRedoAction(nameof(IsEnabled), this,

oldValue, x, x ? "Enable {Name}" : "Disable {Name}"));

});

}

public GameEntity(Scene scene)

{

Debug.Assert(scene != null);

ParentScene = scene;

_components.Add(new Transform(this));

OnDeserialized(new StreamingContext());

}

}

abstract class MSEntity : ViewModelBase

{

private bool _enableUpdates = true;

private bool? _isEnabled;

DataMember

public bool? IsEnabled

{

get => _isEnabled;

set

{

if (_isEnabled != value)

{

_isEnabled = value;

OnPropertyChanged(nameof(IsEnabled));

}

}

}

private string _name;

DataMember

public string Name

{

get => _name;

set

{

if (_name != value)

{

_name = value;

OnPropertyChanged(nameof(Name));

}

}

}

private readonly ObservableCollection<IMSComponent> _components = new ObservableCollection<IMSComponent>();

public ReadOnlyObservableCollection<IMSComponent> Components { get; private set; }

public T GetMSComponent<T>() where T : IMSComponent

{

return (T)Components.FirstOrDefault(x => x.GetType() == typeof(T));

}

public List<GameEntity> SelectedEntities { get; }

private void MakeComponentList()

{

_components.Clear();

var firstEntity = SelectedEntities.FirstOrDefault();

if (firstEntity == null) return;

foreach (var component in firstEntity.Components)

{

var type = component.GetType();

if (!SelectedEntities.Skip(1).Any(entity => entity.GetComponent(type) == null))

{

Debug.Assert(Components.FirstOrDefault(x => x.GetType() == type) == null);

// _components.Add(component.GetMultiselectionComponent(this));

}

}

}

public static float? GetMixedValue<T>(List<T> objects, Func<T, float> getProperty)

{

var value = getProperty(objects.First());

return objects.Skip(1).Any(x => !getProperty(x).IsTheSameAs(value)) ? (float?)null : value;

}

public static bool? GetMixedValue<T>(List<T> objects, Func<T, bool> getProperty)

{

var value = getProperty(objects.First());

return objects.Skip(1).Any(x => value != getProperty(x)) ? (bool?)null : value;

}

public static string? GetMixedValue<T>(List<T> objects, Func<T, string> getProperty)

{

var value = getProperty(objects.First());

return objects.Skip(1).Any(x => value != getProperty(x)) ? null : value;

}

protected virtual bool UpdateGameEntities(string propertyName)

{

switch (propertyName)

{

case nameof(IsEnabled): SelectedEntities.ForEach(x => x.IsEnabled = IsEnabled.Value); return true;

case nameof(Name): SelectedEntities.ForEach(x => x.Name = Name); return true;

}

return false;

}

protected virtual bool UpdateMSGameEntity()

{

IsEnabled = GetMixedValue(SelectedEntities, new Func<GameEntity, bool>(x => x.IsEnabled));

Name = GetMixedValue(SelectedEntities, new Func<GameEntity, string>(x => x.Name));

return true;

}

public void Refresh()

{

_enableUpdates = false;

UpdateMSGameEntity();

MakeComponentList();

_enableUpdates = true;

}

public MSEntity(List<GameEntity> entities)

{

Debug.Assert(!entities.Any() == true);

Components = new ReadOnlyObservableCollection<IMSComponent>(_components);

SelectedEntities = entities;

PropertyChanged += (s, e) => { if (_enableUpdates) UpdateGameEntities(e.PropertyName); };

}

}

class MSGameEntity : MSEntity

{

public MSGameEntity(List<GameEntity> entities) : base(entities)

{

Refresh();

}

}

}

相关推荐
无心水5 小时前
分布式定时任务与SELECT FOR UPDATE:从致命陷阱到优雅解决方案(实战案例+架构演进)
服务器·人工智能·分布式·后端·spring·架构·wpf
LZL_SQ7 小时前
HCCL测试框架中AllReduce边界条件测试设计深度剖析
wpf·cann
小李也疯狂18 小时前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap
呆呆敲代码的小Y18 小时前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表
EQ-雪梨蛋花汤18 小时前
【Unity优化】Unity多场景加载优化与资源释放完整指南:解决Additive加载卡顿、预热、卸载与内存释放问题
unity·游戏引擎
我的offer在哪里19 小时前
用 Unity 从 0 做一个「可以玩的」游戏,需要哪些步骤和流程
游戏·unity·游戏引擎
泡泡茶壶ᐇ20 小时前
Unity游戏开发入门指南:从零开始理解游戏引擎核心概念
unity·游戏引擎
Var_al1 天前
抖小Unity WebGL分包命令行工具实践指南
unity·游戏引擎·webgl
天人合一peng1 天前
unity 通过代码修改button及其名字字体的属性
unity·游戏引擎
GLDbalala1 天前
Unity基于自定义管线实现经典经验光照模型
unity·游戏引擎