撰写了文章 更新于 2019-12-20 20:27:14
《消亡的海》游戏开发日志02-移动
一切都是摸着石头过河,因为只有一个人在开发,且也不想事先策划。
计划先写一些简单的,后面再添加。
今天先完成:
①建个城市;
②生成个角色;
③角色在城市里移动
城市 类
public class City { public string cityName; public int cityId; public int width; // 城市格子数 public int height; // Npc列表 城内的NPC们 public List<Npc> npcList; // Room列表 城内有哪些房间 public List<Room> roomList; } public class Pos { public int xPos; public int yPos; }
角色 类
public class Role { public string uname; public Pos pos; // 角色在城内的坐标 public float xStep; // 在格子内的像素位置 public float yStep; // 状态 public bool isAllowMove; public string faceTo; // 交互的NPC public Npc talkNpc; public Role(string _uname, Pos _pos) { uname = _uname; pos = _pos; xStep = 0; yStep = 0; // 状态 isAllowMove = true; faceTo = "down"; // 交互 talkNpc = null; } public void Move(string _dir, City _city) { if (_dir == "up") { // 超过一格 if (yStep + 1 >= RoleConfig.gridStep) { // 超出城市上界 if (pos.yPos + 1 < _city.height) { yStep = 0; pos.yPos += 1; } // 未超一格 } else { yStep += 1; } faceTo = "up"; } else if (_dir == "down") { // 超过一格 if (yStep - 1 < 0) { // 超出城市下界 if (pos.yPos - 1 >= 0) { yStep = RoleConfig.gridStep; pos.yPos -= 1; } // 未超一格 } else { yStep -= 1; } faceTo = "down"; } else if (_dir == "left") { if (xStep - 1 < 0) { // 超出城市左界 if (pos.xPos - 1 >= 0) { xStep = RoleConfig.gridStep; pos.xPos -= 1; } } else { xStep -= 1; } faceTo = "left"; } else if (_dir == "right") { if (xStep + 1 >= RoleConfig.gridStep) { // 超出城市右界 if (pos.xPos + 1 < _city.width) { xStep = 0; pos.xPos += 1; } } else { xStep += 1; } faceTo = "right"; } } }
游戏数据 类
public class GameData { static GameData instance; public static GameData Instance { get { if (instance == null) { instance = new GameData(); } return instance; } } public City city; public Role role; public void CreateGame() { city = new City("出生城", 0, 10, 10); role = new Role("白沙", new Pos(2, 3)); } }
Unity的操作控制类
public class ControlManager : MonoBehaviour { public static ControlManager Instance; void Awake() { if (Instance == null) Instance = this; } void Update() { Move(); } void Move() { Role role = App.Instance.role; if (role == null) return; if (role.isAllowMove == false) return; bool _up = Input.GetKey(KeyCode.W); bool _down = Input.GetKey(KeyCode.S); bool _left = Input.GetKey(KeyCode.A); bool _right = Input.GetKey(KeyCode.D); if(_up) { role.Move("up", App.Instance.city); } if (_down) { role.Move("down", App.Instance.city); } if (_left) { role.Move("left", App.Instance.city); } if (_right) { role.Move("right", App.Instance.city); } } }
数据测试 类
public class TestManager : MonoBehaviour { public static TestManager Instance; public Text roleText; public Text cityText; public Text npcText; void Awake() { if (Instance == null) Instance = this; } void Start() { } void Update() { ShowText(); } void ShowText() { ShowRole(); ShowCity(); } void ShowRole() { Role role = App.Instance.role; if (role == null) return; roleText.text = "角色名:" + role.uname + "\n步标:" + role.xStep.ToString() + "," + role.yStep.ToString() + "\n坐标:" + role.pos.xPos.ToString() + "," + role.pos.yPos.ToString(); if (role.talkNpc == null) return; roleText.text += "\n当前互动NPC:" + role.talkNpc.nname; } void ShowCity() { City city = App.Instance.city; if (city == null) return; cityText.text = "城市名:" + city.cityName + "\n城市宽高:" + city.width.ToString() + "," + city.height.ToString(); npcText.text = ""; foreach (Npc _npc in city.npcList) { npcText.text += "\n \nNPC名:" + _npc.nname + "\n坐标:" + _npc.pos.xPos.ToString() + "," + _npc.pos.yPos.ToString(); } } }
现在看看角色移动效果
按WSAD走走看,嗯,角色动起来了。(动图太大就只放一小段)