Skip to content

Commit a3bd12c

Browse files
committed
增加数据局部性模式 | add Data Locality Pattern
增加数据局部性模式 | add Data Locality Pattern
1 parent 340ead7 commit a3bd12c

File tree

6 files changed

+178
-0
lines changed

6 files changed

+178
-0
lines changed

Assets/Game Programming Patterns/Data Locality Pattern.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Game Programming Patterns/Data Locality Pattern/Example.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
//-------------------------------------------------------------------------------------
2+
// DataLocalityPatternExample.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
using System;
8+
9+
namespace DataLocalityPatternExample
10+
{
11+
public class DataLocalityPatternExample : MonoBehaviour
12+
{
13+
GameX gameProject;
14+
15+
void Start()
16+
{
17+
gameProject = new GameX();
18+
gameProject.Start();
19+
}
20+
21+
void Update()
22+
{
23+
if (gameProject!=null)
24+
{
25+
gameProject.Update();
26+
}
27+
}
28+
}
29+
30+
/// <summary>
31+
/// 游戏应用程序类
32+
/// </summary>
33+
public class GameX
34+
{
35+
const int MAX_ENTITIES = 10000;
36+
37+
int numEntities;
38+
39+
/// <summary>
40+
/// 基于大数组存储保证数据连续性
41+
/// </summary>
42+
AIComponent[] aiComponents = new AIComponent[MAX_ENTITIES];
43+
PhysicsComponent[] physicsComponents = new PhysicsComponent[MAX_ENTITIES];
44+
RenderComponent[] renderComponents = new RenderComponent[MAX_ENTITIES];
45+
46+
public void Start()
47+
{
48+
numEntities = 10;
49+
for (int i = 0; i < numEntities; i++)
50+
{
51+
aiComponents[i] = new AIComponent();
52+
physicsComponents[i] = new PhysicsComponent();
53+
renderComponents[i] = new RenderComponent();
54+
}
55+
56+
}
57+
58+
59+
public void Update()
60+
{
61+
// Process AI.
62+
for (int i = 0; i < numEntities; i++)
63+
{
64+
if (aiComponents!=null && aiComponents.Length>i && aiComponents[i]!= null)
65+
{
66+
aiComponents[i].Update();
67+
}
68+
}
69+
70+
// Update physics.
71+
for (int i = 0; i < numEntities; i++)
72+
{
73+
if (physicsComponents != null && physicsComponents.Length > i && physicsComponents[i] != null)
74+
{
75+
physicsComponents[i].Update();
76+
}
77+
78+
}
79+
80+
// Draw to screen.
81+
for (int i = 0; i < numEntities; i++)
82+
{
83+
if (renderComponents != null && renderComponents.Length > i && renderComponents[i] != null)
84+
{
85+
renderComponents[i].Render();
86+
}
87+
}
88+
}
89+
90+
91+
92+
/// <summary>
93+
/// 组件接口
94+
/// </summary>
95+
public interface IComponent
96+
{
97+
void Update();
98+
}
99+
100+
/// <summary>
101+
/// AI组件
102+
/// </summary>
103+
public class AIComponent : IComponent
104+
{
105+
public void Update()
106+
{
107+
Debug.Log("AIComponent Update!");
108+
}
109+
}
110+
111+
/// <summary>
112+
/// 物理组件
113+
/// </summary>
114+
public class PhysicsComponent : IComponent
115+
{
116+
public void Update()
117+
{
118+
Debug.Log("PhysicsComponent Update!");
119+
}
120+
}
121+
122+
/// <summary>
123+
/// 渲染组件
124+
/// </summary>
125+
public class RenderComponent : IComponent
126+
{
127+
public void Update()
128+
{
129+
Debug.Log("RenderComponent Update!");
130+
}
131+
132+
133+
public void Render()
134+
{
135+
Debug.Log("RenderComponent Render!");
136+
}
137+
}
138+
139+
}
140+
}

Assets/Game Programming Patterns/Data Locality Pattern/Example/DataLocalityPatternExample.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Game Programming Patterns/Data Locality Pattern/Example/DataLocalityPatternExample.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)