Created
February 21, 2019 14:34
-
-
Save tsubaki/0002af1cccbd03529840c3f2d69b90bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
using Unity.Transforms; | |
using Unity.Collections; | |
using Unity.Mathematics; | |
public class NewComponentDataArraySampleSystem : ComponentSystem | |
{ | |
ComponentGroup playerGroup, enemyGroup; | |
protected override void OnCreateManager() | |
{ | |
// コンポーネントの準備 | |
playerGroup = GetComponentGroup( | |
ComponentType.ReadOnly<Position>(), | |
ComponentType.ReadOnly<Player>(), | |
ComponentType.Create<HitPoint>()); | |
enemyGroup = GetComponentGroup( | |
ComponentType.ReadOnly<Position>(), | |
ComponentType.ReadOnly<Enemy>()); | |
RequireForUpdate(playerGroup); | |
RequireForUpdate(enemyGroup); | |
} | |
protected override void OnUpdate() | |
{ | |
// チャンクからNativeArrayを取得 | |
var playerPositions = playerGroup.ToComponentDataArray<Position>(Allocator.Persistent); | |
var playerHitpoint = playerGroup.ToComponentDataArray<HitPoint>(Allocator.TempJob); | |
var enemyPositions = enemyGroup.ToComponentDataArray<Position>(Allocator.TempJob); | |
// 一定距離になったらPlayerにダメージ的な例 | |
for (int enemyIndex = 0; enemyIndex < enemyPositions.Length; enemyIndex++) | |
{ | |
for (int playerIndex = 0; playerIndex < playerPositions.Length; playerIndex++) | |
{ | |
if( math.distance( enemyPositions[enemyIndex].Value, playerPositions[playerIndex].Value) < 1) | |
{ | |
var hitpoint = playerHitpoint[playerIndex]; | |
hitpoint.Value -= 1; | |
playerHitpoint[playerIndex] = hitpoint; | |
} | |
} | |
} | |
// ダメージを反映 | |
playerGroup.CopyFromComponentDataArray(playerHitpoint); | |
// お片付け | |
playerPositions.Dispose(); | |
playerHitpoint.Dispose(); | |
enemyPositions.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment