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