113
社区成员
发帖
与我相关
我的任务
分享| 这个作业属于哪个课程 | 2401_CS_SE_FZU社区-CSDN社区云 |
|---|---|
| 这个作业要求在哪里 | 团队作业—bate冲刺+事后诸葛亮-CSDN社区 |
| 这个作业的目标 | beta冲刺代码规范 |
| 其他参考文献 | 无 |
Assets/Scripts/Controllers/PlayerController.csAssets/Scripts/Helpers/MathExtensions.csAssets/Scripts/Managers/GameManager.csAssets/Scripts/Models/PlayerData.cs缩进和空格:
public class PlayerController : MonoBehaviour
{
private int health;
private void Update()
{
if (health <= 0)
{
Debug.Log("Player is dead");
}
}
public void TakeDamage(int amount)
{
health -= amount;
}
}
大括号风格:
if (condition)
{
// code
}
else
{
// code
}
public class MyClass
{
// code
}
文件头注释:
// Author: John Doe
// Created Date: 2024-01-01
// Description: Controls player movement and actions
方法注释:
/// <summary>
/// Deals damage to the player.
/// </summary>
/// <param name="amount">The amount of damage to deal.</param>
public void TakeDamage(int amount)
{
health -= amount;
}
类和接口:
public class PlayerController : MonoBehaviour
{
// code
}
方法:
public void UpdateHealth(int amount)
{
health = Mathf.Clamp(health + amount, 0, MaxHealth);
}
属性和字段:
private int health;
private float speed;
局部变量:
for (int i = 0; i < items.Length; i++)
{
// code
}
常量:
public const int MaxHealth = 100;
**使用var**:
var player = FindObjectOfType<PlayerController>();
避免魔法数字:
public const int MaxHealth = 100;
public void HealPlayer(int amount)
{
health += amount;
health = Mathf.Min(health, MaxHealth);
}
字符串连接:
string playerName = "Player";
string message = $"Welcome, {playerName}!";
Debug.Log(message);
异常处理:
public void LoadGame()
{
try
{
// Code that might throw an exception
}
catch (Exception ex)
{
Debug.LogError("Failed to load game: " + ex.Message);
}
}
避免在Update中使用:
private void Start()
{
StartCoroutine(LoadData());
}
private IEnumerator LoadData()
{
// Simulate data loading with a delay
yield return new WaitForSeconds(2);
// Load data
}
对象池:
public class ObjectPool : MonoBehaviour
{
public List<GameObject> pool;
public GameObject prefab;
public GameObject GetObject()
{
if (pool.Count > 0)
{
return pool[0];
}
else
{
return Instantiate(prefab);
}
}
public void ReturnObject(GameObject obj)
{
pool.Add(obj);
}
}
单一职责原则:
// PlayerInputHandler is responsible for handling input
public class PlayerInputHandler : MonoBehaviour
{
public void MovePlayer(Vector3 direction)
{
// Move player logic
}
}
// PlayerController is responsible for player state and logic
public class PlayerController : MonoBehaviour
{
private PlayerInputHandler inputHandler;
private void Start()
{
inputHandler = GetComponent<PlayerInputHandler>();
}
private void Update()
{
Vector3 moveDirection = GetMoveDirection();
inputHandler.MovePlayer(moveDirection);
}
private Vector3 GetMoveDirection()
{
// Determine move direction based on input
return new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
}
}
DRY原则:
public static class MathExtensions
{
public static int Clamp(int value, int min, int max)
{
return Math.Max(min, Math.Min(max, value));
}
}
public class PlayerController : MonoBehaviour
{
private void UpdateHealth(int amount)
{
health = MathExtensions.Clamp(health + amount, 0, MaxHealth);
}
}
提交信息:
git commit -m "Add new feature: player sprinting"
分支策略:
main:稳定版本,用于发布。develop:开发版本,用于日常开发。feature/xxx:新功能开发。bugfix/xxx:修复bug。单元测试:
[TestFixture]
public class PlayerTests
{
[Test]
public void Health_ShouldBeFullAfterRest()
{
// Arrange
Player player = new Player();
player.Health = 50;
// Act
player.Rest();
// Assert
Assert.AreEqual(100, player.Health);
}
}
代码审查:
定期进行代码审查会议,讨论代码改进和最佳实践。