浏览代码

Updated code to match video

/main
Mike Geig 5 年前
当前提交
2a396177
共有 5 个文件被更改,包括 41 次插入49 次删除
  1. 8
      DOTS_Pong/Assets/Scripts/GameManager.cs
  2. 39
      DOTS_Pong/Assets/Scripts/Systems/BallGoalCheckSystem.cs
  3. 21
      DOTS_Pong/Assets/Scripts/Systems/IncreaseVelocityOverTimeSystem.cs
  4. 17
      DOTS_Pong/Assets/Scripts/Systems/PaddleMovementSystem.cs
  5. 5
      DOTS_Pong/Assets/Scripts/Systems/PlayerInputSystem.cs

8
DOTS_Pong/Assets/Scripts/GameManager.cs


main = this;
playerScores = new int[2];
manager = World.DefaultGameObjectInjectionWorld.EntityManager;
manager = World.DefaultGameObjectInjectionWorld.EntityManager;
oneSecond = new WaitForSeconds(1f);
delay = new WaitForSeconds(respawnDelay);

public void PlayerScored(int playerID)
{
playerScores[playerID]++;
for(int i = 0; i < playerScores.Length && i < playerTexts.Length; i++)
for (int i = 0; i < playerScores.Length && i < playerTexts.Length; i++)
playerTexts[i].text = playerScores[i].ToString();
StartCoroutine(CountdownAndSpawnBall());

void SpawnBall()
{
Entity ball = manager.Instantiate(ballEntityPrefab);
Vector3 dir = new Vector3(UnityEngine.Random.Range(0, 2) == 0 ? -1 : 1, UnityEngine.Random.Range(-.5f, .5f), 0f).normalized;
Vector3 speed = dir * ballSpeed;

39
DOTS_Pong/Assets/Scripts/Systems/BallGoalCheckSystem.cs


using Unity.Mathematics;
using Unity.Jobs;
[AlwaysSynchronizeSystem]
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
inputDeps.Complete();
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
.WithAll<BallTag>()
.WithAll<BallTag>()
.ForEach((Entity entity, in Translation trans) =>
{
float3 pos = trans.Value;
float bound = GameManager.main.xBound;
.ForEach((Entity entity, in Translation trans) =>
{
float3 pos = trans.Value;
float bound = GameManager.main.xBound;
if (pos.x >= bound)
{
GameManager.main.PlayerScored(0);
if (pos.x >= bound)
{
GameManager.main.PlayerScored(0);
}
else if (pos.x <= -bound)
{
GameManager.main.PlayerScored(1);
}
else if (pos.x <= -bound)
{
GameManager.main.PlayerScored(1);
}
}).Run();
}
}).Run();
return new JobHandle();
}
return default;
}
}

21
DOTS_Pong/Assets/Scripts/Systems/IncreaseVelocityOverTimeSystem.cs


using Unity.Mathematics;
using Unity.Physics;
[AlwaysSynchronizeSystem]
inputDeps.Complete();
Entities
.ForEach((ref PhysicsVelocity vel, in SpeedIncreaseOverTimeData data) =>
{
var modifier = new float2(data.increasePerSecond * deltaTime);
float2 newVel = vel.Linear.xy;
newVel += math.lerp(-modifier, modifier, math.sign(newVel));
Entities.ForEach((ref PhysicsVelocity vel, in SpeedIncreaseOverTimeData data) =>
{
float2 modifier = new float2(data.increasePerSecond * deltaTime);
vel.Linear.xy = newVel;
}).Run();
float2 newVel = vel.Linear.xy;
newVel += math.lerp(-modifier, modifier, math.sign(newVel));
vel.Linear.xy = newVel;
}).Run();
return new JobHandle();
return default;
}
}

17
DOTS_Pong/Assets/Scripts/Systems/PaddleMovementSystem.cs


using Unity.Mathematics;
using Unity.Transforms;
[AlwaysSynchronizeSystem]
inputDeps.Complete();
Entities
.ForEach((ref Translation trans, in PaddleMovementData data) =>
{
trans.Value.y = math.clamp(trans.Value.y + (data.speed * data.direction * deltaTime), -yBound, yBound);
}).Run();
Entities.ForEach((ref Translation trans, in PaddleMovementData data) =>
{
trans.Value.y = math.clamp(trans.Value.y + (data.speed * data.direction * deltaTime), -yBound, yBound);
}).Run();
return new JobHandle();
return default;
}
}

5
DOTS_Pong/Assets/Scripts/Systems/PlayerInputSystem.cs


using Unity.Jobs;
using UnityEngine;
[AlwaysSynchronizeSystem]
inputDeps.Complete();
Entities.ForEach((ref PaddleMovementData moveData, in PaddleInputData inputData) =>
{
moveData.direction = 0;

}).Run();
return new JobHandle();
return default;
}
}
正在加载...
取消
保存