Skip to content

Commit

Permalink
同步子弹
Browse files Browse the repository at this point in the history
  • Loading branch information
Caizc committed Sep 21, 2017
1 parent 18b8bab commit 838e1fb
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 17 deletions.
Binary file modified Assets/Prefabs/Bolts/Bolt.prefab
Binary file not shown.
Binary file modified Assets/Prefabs/Environment.prefab
Binary file not shown.
12 changes: 12 additions & 0 deletions Assets/Scripts/gameplay/SyncedDestoryByBoundary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using TrueSync;

/// <summary>
/// 销毁离开游戏边界的对象
/// </summary>
public class SyncedDestoryByBoundary : TrueSyncBehaviour
{
public void OnSyncedTriggerExit(TSCollision other)
{
Destroy(other.gameObject);
}
}
12 changes: 12 additions & 0 deletions Assets/Scripts/gameplay/SyncedDestoryByBoundary.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 29 additions & 4 deletions Assets/Scripts/gameplay/SyncedHealth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ public class SyncedHealth : TrueSyncBehaviour
[SerializeField] private GameObject playerExplosion;

/// <summary>
/// 角色生命值/物品耐久度
/// 当前角色生命值/物品耐久度
/// </summary>
[AddTracking] public int Health = 100;

// 临时保存最大生命值/耐久度
private int _maxHealth;

public override void OnSyncedStart()
{
_maxHealth = Health;
}

/// <summary>
Expand All @@ -37,6 +41,14 @@ public void TakeDamage(int damage)
}
}

/// <summary>
/// 重置
/// </summary>
public void Reset()
{
Health = _maxHealth;
}

/// <summary>
/// 播放视觉特效和声音特效
/// </summary>
Expand All @@ -53,11 +65,24 @@ private void PlayFX()
/// </summary>
private void Death()
{
TrueSyncManager.SyncedDestroy(this.gameObject);
if (this.gameObject.tag == "Player")
{
// 如果该对象是玩家角色,则播放玩家角色爆炸特效,并进行重生处理
if (null != playerExplosion)
{
Instantiate(playerExplosion, transform.position, transform.rotation);
}

if (null != playerExplosion)
SyncedPlayerController playerController = GetComponent<SyncedPlayerController>();
if (playerController != null)
{
playerController.Respawn();
}
}
else
{
Instantiate(playerExplosion, transform.position, transform.rotation);
// 否则,同步销毁本对象
TrueSyncManager.SyncedDestroy(this.gameObject);
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/gameplay/SyncedInputManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using TrueSync;
using UnityEngine;

/// <summary>
/// 输入管理
/// </summary>
public class SyncedInputManager : MonoBehaviour
{
// 移动增量
Expand Down
26 changes: 26 additions & 0 deletions Assets/Scripts/gameplay/SyncedMover.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using TrueSync;
using UnityEngine;

/// <summary>
/// 匀速移动
/// </summary>
public class SyncedMover : TrueSyncBehaviour
{
[SerializeField] private FP speed = 20;

private TSRigidBody _rigidbody;

public override void OnSyncedStart()
{
_rigidbody = GetComponent<TSRigidBody>();

if (_rigidbody != null)
{
_rigidbody.velocity = tsTransform.forward * speed;
}
else
{
Debug.LogError("Missing TSRigidBody on current GameObject!");
}
}
}
12 changes: 12 additions & 0 deletions Assets/Scripts/gameplay/SyncedMover.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 22 additions & 8 deletions Assets/Scripts/gameplay/SyncedPlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public class BoundaryFP
public FP xMin, xMax, zMin, zMax;
}

/// <summary>
/// 玩家角色控制器
/// </summary>
public class SyncedPlayerController : TrueSyncBehaviour
{
// 移动速度
Expand All @@ -17,16 +20,23 @@ public class SyncedPlayerController : TrueSyncBehaviour
// 转向速度
[SerializeField] private FP rotateSpeed;

// 移动范围界限
[SerializeField] private BoundaryFP boundary;

// 机身倾斜系数
[SerializeField] private FP tilt;

// 子弹 Prefab 和枪口位置
[SerializeField] private GameObject shot;

[SerializeField] private Transform shotSpawn;

// 开火间隔
[SerializeField] private FP fireDelta = 0.25;

// 引擎特效粒子系统
[SerializeField] private ParticleSystem particleSystem1;

[SerializeField] private ParticleSystem particleSystem2;

private SyncedInputManager _syncedInputManager;
Expand Down Expand Up @@ -108,8 +118,6 @@ public override void OnSyncedInput()

public override void OnSyncedUpdate()
{
// TODO: 收到更新的操控指令

_movX = TrueSyncInput.GetFP(0);
_movY = TrueSyncInput.GetFP(1);
_rotX = TrueSyncInput.GetFP(2);
Expand Down Expand Up @@ -147,7 +155,6 @@ private void Move()
// 旋转方向
if (_rotX == 0 && _rotY == 0)
{

tsTransform.rotation = TSQuaternion.Lerp(tsTransform.rotation, TSQuaternion.identity,
TrueSyncManager.DeltaTime * rotateSpeed);
}
Expand Down Expand Up @@ -194,15 +201,22 @@ private void Fire()
}
}

private void UpdateHealth()
/// <summary>
/// 重生
/// </summary>
public void Respawn()
{
_healthText.text = "Health: " + _syncedHealth.Health;
// 死亡次数加一
_death++;
// 重置生命值
_syncedHealth.Reset();
// 设置随机出生位置
tsTransform.position = new TSVector(TSRandom.Range(-5, 5), 0, TSRandom.Range(-5, 5));
}

// TODO
public void Respawn()
private void UpdateHealth()
{
tsTransform.position = new TSVector(TSRandom.Range(-5, 5), 0, TSRandom.Range(-5, 5));
_healthText.text = "Health: " + _syncedHealth.Health;
}

void OnGUI()
Expand Down
6 changes: 1 addition & 5 deletions Assets/_HelloWorld/Projectile.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using TrueSync;
using UnityEngine;
using TrueSync;

public class Projectile : TrueSyncBehaviour
{
Expand Down
Binary file modified Assets/_Scenes/Battle.unity
Binary file not shown.

0 comments on commit 838e1fb

Please sign in to comment.