Explorar o código

增加类似unity3d的组件设计

tanghai %!s(int64=11) %!d(string=hai) anos
pai
achega
4f938275a3

+ 49 - 49
CSharp/App/Modules/BehaviorTreeModule/AllTreeView.xaml

@@ -1,55 +1,55 @@
-<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-		xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
-		xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
-		xmlns:Tree="clr-namespace:Modules.BehaviorTreeModule"
-		x:Class="Modules.BehaviorTreeModule.AllTreeView"
-		mc:Ignorable="d" 
-		d:DesignHeight="800" 
-		d:DesignWidth="1280" 
-		d:DataContext="{d:DesignInstance Tree:AllTreeViewModel}">
-	<DockPanel>
-	<Menu DockPanel.Dock="Top" Height="24">
-		<MenuItem Header="File">
-			<MenuItem Header="打开" Click="MenuItem_Open" />
-			<MenuItem Header="保存" Click="MenuItem_Save" />
-		</MenuItem>
-	</Menu>
-	<Grid>
-		<Grid.RowDefinitions>
+<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+		xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+		xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
+		xmlns:Tree="clr-namespace:Modules.BehaviorTreeModule"
+		x:Class="Modules.BehaviorTreeModule.AllTreeView"
+		mc:Ignorable="d" 
+		d:DesignHeight="800" 
+		d:DesignWidth="1280" 
+		d:DataContext="{d:DesignInstance Tree:AllTreeViewModel}">
+	<DockPanel>
+	<Menu DockPanel.Dock="Top" Height="24">
+		<MenuItem Header="File">
+			<MenuItem Header="打开" Click="MenuItem_Open" />
+			<MenuItem Header="保存" Click="MenuItem_Save" />
+		</MenuItem>
+	</Menu>
+	<Grid>
+		<Grid.RowDefinitions>
 			<RowDefinition/>
 			<RowDefinition/>
-		</Grid.RowDefinitions>
-		<Grid.ColumnDefinitions>
-			<ColumnDefinition Width="230"/>
+		</Grid.RowDefinitions>
+		<Grid.ColumnDefinitions>
+			<ColumnDefinition Width="230"/>
 			<ColumnDefinition Width="Auto"/>
-			<ColumnDefinition />
-		</Grid.ColumnDefinitions>
-		<GroupBox Grid.Row="0" Grid.Column="0" Header="行为树列表:">
-			<ListBox Name="lbTreeRoots" ItemsSource="{Binding RootList}" VerticalAlignment="Stretch" >
-				<ListBox.ContextMenu>
-					<ContextMenu>
-						<MenuItem Header="新建树" Click="MenuItem_New" />
-						<MenuItem Header="删除树" Click="MenuItem_Remove" />
-					</ContextMenu>
-				</ListBox.ContextMenu>
-				<ListBox.ItemTemplate>
-					<DataTemplate DataType="Tree:TreeNodeViewModel">
-						<StackPanel MouseLeftButtonDown="ListBoxItem_OnMouseLeftButtonDown" MouseLeftButtonUp="ListBoxItem_OnMouseLeftButtonUp" 
-									Orientation="Horizontal" Margin="1,0">
+			<ColumnDefinition />
+		</Grid.ColumnDefinitions>
+		<GroupBox Grid.Row="0" Grid.Column="0" Header="行为树列表:">
+			<ListBox Name="lbTreeRoots" ItemsSource="{Binding RootList}" VerticalAlignment="Stretch" >
+				<ListBox.ContextMenu>
+					<ContextMenu>
+						<MenuItem Header="新建树" Click="MenuItem_New" />
+						<MenuItem Header="删除树" Click="MenuItem_Remove" />
+					</ContextMenu>
+				</ListBox.ContextMenu>
+				<ListBox.ItemTemplate>
+					<DataTemplate DataType="Tree:TreeNodeViewModel">
+						<StackPanel MouseLeftButtonDown="ListBoxItem_OnMouseLeftButtonDown" MouseLeftButtonUp="ListBoxItem_OnMouseLeftButtonUp" 
+									Orientation="Horizontal" Margin="1,0">
 							<Label Content="{Binding TreeId}" VerticalAlignment="Stretch" Width="20"/>
 							<Label Content="{Binding Comment}" VerticalAlignment="Stretch" Width="150"/>
-						</StackPanel>
-					</DataTemplate>
-				</ListBox.ItemTemplate>
-			</ListBox>
-		</GroupBox>
-		<GroupBox Grid.Row="1" Grid.Column="0" Header="节点编辑:">
-			<Tree:NodeDataEditor x:Name="nodeDataEditor" VerticalAlignment="Top" />
-		</GroupBox>
-		<GridSplitter Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" Width="3" ShowsPreview="False" VerticalAlignment="Stretch"
-				HorizontalAlignment="Stretch" />
-		<Tree:TreeView x:Name="treeView" Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" />
-		</Grid>
-	</DockPanel>
+						</StackPanel>
+					</DataTemplate>
+				</ListBox.ItemTemplate>
+			</ListBox>
+		</GroupBox>
+		<GroupBox Grid.Row="1" Grid.Column="0" Header="节点编辑:">
+			<Tree:NodeDataEditor x:Name="nodeDataEditor" VerticalAlignment="Top" />
+		</GroupBox>
+		<GridSplitter Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" Width="3" ShowsPreview="False" VerticalAlignment="Stretch"
+				HorizontalAlignment="Stretch" />
+		<Tree:TreeView x:Name="treeView" Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" />
+		</Grid>
+	</DockPanel>
 </UserControl>

+ 1 - 1
CSharp/Platform/Common/Base/Component.cs

@@ -2,6 +2,6 @@
 {
     public class Component: Object
     {
-        public Object Parent { get; set; }
+        public Entity Owner { get; set; }
     }
 }

+ 47 - 0
CSharp/Platform/Common/Base/Entity.cs

@@ -0,0 +1,47 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Common.Base
+{
+    public class Entity: Object
+    {
+        private readonly Dictionary<string, Component> components = new Dictionary<string, Component>();
+
+        public void AddComponent<T>() where T : Component, new()
+        {
+            T t = new T { Owner = this };
+            this.components.Add(typeof(T).Name, t);
+        }
+
+        public void RemoveComponent<T>() where T : Component
+        {
+            this.components.Remove(typeof(T).Name);
+        }
+
+        public T GetComponent<T>() where T : Component
+        {
+            Component t;
+            if (!this.components.TryGetValue(typeof (T).Name, out t))
+            {
+                throw new Exception(string.Format("not found component: {0}", typeof(T).Name));
+            }
+            return (T) t;
+        }
+
+        public T TryGetComponent<T>() where T : Component
+        {
+            Component t;
+            if (!this.components.TryGetValue(typeof(T).Name, out t))
+            {
+                return null;
+            }
+            return (T) t;
+        }
+
+        public Component[] GetComponents()
+        {
+            return this.components.Values.ToArray();
+        }
+    }
+}

+ 1 - 0
CSharp/Platform/Common/Common.csproj

@@ -58,6 +58,7 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Base\Entity.cs" />
     <Compile Include="Base\Component.cs" />
     <Compile Include="Config\ICategory.cs" />
     <Compile Include="Event\EventTrigger.cs" />