public void Init(int row, int col, MapCtr map) { m_isGround = false; m_row = row; m_col = col; m_mapCtr = map; m_curCell = m_mapCtr.GetMapCell(row, col); transform.position = m_curCell.Position; m_ani = GetComponentInChildren <Animator> (); m_localPositionTemp = m_body.transform.localPosition; }
public void Init(int row, int col, MapCtr map) { m_isMoving = false; m_row = row; m_col = col; m_dir = new Vector3(1, 0, 0); m_mapCtr = map; m_curCell = m_mapCtr.GetMapCell(row, col); transform.position = m_curCell.Position; m_ani = GetComponentInChildren <Animator> (); CheckGround(); }
void FixedUpdate() { if (!CanControl) { return; } //上格子 Vector2 upPos = new Vector2(transform.position.x, transform.position.y + (float)m_mapCtr.Map.cellHeight * 0.5f); MapCellCtr upCell = m_mapCtr.GetMapCell(upPos); CELL_TYPE upCellType = (CELL_TYPE)upCell.CellData.cellType; //下 MapCellCtr downCell = m_mapCtr.GetMapCell(new Vector2(transform.position.x, transform.position.y - (float)m_mapCtr.Map.cellHeight * 0.5f)); CELL_TYPE downCellType = (CELL_TYPE)downCell.CellData.cellType; //右 Vector2 rightPos = new Vector2(transform.position.x + (float)m_mapCtr.Map.cellWidth * 0.5f, transform.position.y); MapCellCtr rightCell = m_mapCtr.GetMapCell(rightPos); CELL_TYPE rightCellType = (CELL_TYPE)rightCell.CellData.cellType; //左 Vector2 leftPos = new Vector2(transform.position.x - (float)m_mapCtr.Map.cellWidth * 0.5f, transform.position.y); MapCellCtr leftCell = m_mapCtr.GetMapCell(leftPos); CELL_TYPE leftCellType = (CELL_TYPE)leftCell.CellData.cellType; //foot MapCellCtr footCell = m_mapCtr.GetMapCell(new Vector2(transform.position.x, transform.position.y - (float)m_mapCtr.Map.cellHeight * 0.5f + 0.1f)); CELL_TYPE footCellType = (CELL_TYPE)footCell.CellData.cellType; //cur m_curCell = m_mapCtr.GetMapCell(new Vector2(transform.position.x, transform.position.y)); CELL_TYPE curCellType = (CELL_TYPE)m_curCell.CellData.cellType; m_row = m_curCell.CellData.row; m_col = m_curCell.CellData.col; //检查是否劈砖 float angleY = m_body.transform.localRotation.eulerAngles.y; angleY = angleY > 180 ? (angleY - 360):angleY; if (angleY > 10) { m_look = 1; } else if (angleY < -10) { m_look = -1; } else { m_look = 0; } if (m_isHitInput) { m_isHitInput = false; if (downCellType == CELL_TYPE.WALL || downCellType == CELL_TYPE.STONE) { if (m_look != 0) { MapCellCtr target = m_mapCtr.GetMapCell(m_row, m_col + m_look); if ((CELL_TYPE)target.CellData.cellType == CELL_TYPE.NONE) { MapCellCtr targetDown = m_mapCtr.GetMapCell(m_row - 1, m_col + m_look); if ((CELL_TYPE)targetDown.CellData.cellType == CELL_TYPE.WALL) { m_isAttack = true; m_ani.SetBool("run", false); m_ani.SetBool("attack", m_isAttack); targetDown.ChangeToNone(true); AudioManager.Instance.PlayAudio("attack"); } } } } } if (m_isAttack) { return; } //计算重力 m_verticalAccelerate += m_g; if (m_verticalAccelerate > 0) { if (upCellType == CELL_TYPE.STONE || upCellType == CELL_TYPE.WALL) { m_verticalAccelerate = 0; } if (footCellType == CELL_TYPE.POLE) { m_verticalAccelerate = m_g; m_isGround = true; } } else if (m_verticalAccelerate < 0) { if (downCellType == CELL_TYPE.STONE || downCellType == CELL_TYPE.WALL || downCellType == CELL_TYPE.LADDER || curCellType == CELL_TYPE.POLE) { m_verticalAccelerate = 0; m_isGround = true; } else { m_isGround = false; m_body.transform.localEulerAngles = new Vector3(0, m_body.transform.localEulerAngles.y, 0); } // if (downCellType == CELL_TYPE.STONE || downCellType == CELL_TYPE.WALL) { transform.position = new Vector3(transform.position.x, downCell.Position.y + (float)m_mapCtr.Map.cellHeight - 0.01f, transform.position.z); } } else { m_isGround = true; } //计算左右位移和上下楼梯 if (m_dir.x > 0) { if (rightCellType == CELL_TYPE.STONE || rightCellType == CELL_TYPE.WALL) { m_dir = new Vector2(0, m_dir.y); } } else if (m_dir.x < 0) { if (leftCellType == CELL_TYPE.STONE || leftCellType == CELL_TYPE.WALL) { m_dir = new Vector2(0, m_dir.y); } } // if (m_dir.y > 0) { if (footCellType != CELL_TYPE.LADDER) { m_dir = new Vector2(m_dir.x, 0); } } else { if (downCellType == CELL_TYPE.STONE || downCellType == CELL_TYPE.WALL) { m_dir = new Vector2(m_dir.x, 0); } } //最后综合考虑重力和输入的上下左右 Vector2 velocity = m_verticalAccelerate * new Vector2(0, 1) * Time.fixedDeltaTime + m_dir * m_moveSpeed; Vector2 dist = velocity * Time.fixedDeltaTime; transform.position += new Vector3(dist.x, dist.y, 0); //animation //是否播放run动画 if (Mathf.Abs(m_dir.x) > 0 && m_isGround) { if ((footCellType != CELL_TYPE.LADDER && footCellType != CELL_TYPE.POLE) || (downCellType == CELL_TYPE.WALL || downCellType == CELL_TYPE.STONE)) { m_isRun = true; } else { m_isRun = false; } } else { m_isRun = false; } //是否播放climb动画 m_isPole = false; if (m_isGround && (footCellType == CELL_TYPE.LADDER || curCellType == CELL_TYPE.POLE)) { if (downCellType == CELL_TYPE.STONE || downCellType == CELL_TYPE.WALL) { m_isClimb = false; } else { m_isClimb = true; if (curCellType == CELL_TYPE.POLE) { m_isPole = true; } } } // else if(m_isGround && curCellType == CELL_TYPE.POLE && Mathf.Abs(m_dir.x) > 0){ // m_isClimb = true; // } else { m_isClimb = false; } if (m_isGround && dist == Vector2.zero && m_isClimb) { m_ani.speed = 0; } else { m_ani.speed = 1; } m_ani.SetBool("isGround", m_isGround); m_ani.SetBool("climb", m_isClimb); if (m_isClimb) { if (Mathf.Abs(m_dir.y) > 0) { AudioManager.Instance.PlayAudio("climb", true); } else { AudioManager.Instance.StopAudio("climb"); } if (m_isPole) { //朝左爬 if (velocity.x > 0.001f) { m_body.transform.localEulerAngles = new Vector3(-90, 90, 180); m_body.transform.localPosition = new Vector3(-0.5f, m_localPositionTemp.y * 0.5f, m_localPositionTemp.z); } else if (velocity.x < -0.001f) //朝右爬 { m_body.transform.localEulerAngles = new Vector3(-90, 90, 0); m_body.transform.localPosition = new Vector3(0.5f, m_localPositionTemp.y * 0.5f, m_localPositionTemp.z); } else { //未输入左右参数,且还未进行体位旋转 if (Mathf.Abs(m_body.transform.localPosition.x - 0.5f) > 0.01f) { m_body.transform.localEulerAngles = new Vector3(-90, 90, 180); m_body.transform.localPosition = new Vector3(-0.5f, m_localPositionTemp.y * 0.5f, m_localPositionTemp.z); } } } else { m_body.transform.localPosition = m_localPositionTemp; m_body.transform.localEulerAngles = new Vector3(0, 0, 0); } } else { AudioManager.Instance.StopAudio("climb"); m_body.transform.localPosition = m_localPositionTemp; //m_body.transform.localEulerAngles = new Vector3 (0, 90, 0); } m_ani.SetBool("run", m_isRun); if (m_isRun) { //AudioManager.Instance.PlayAudio("run", true); if (velocity.x > 0) { m_body.transform.localEulerAngles = new Vector3(0, 90, 0); } else { m_body.transform.localEulerAngles = new Vector3(0, -90, 0); } } else { //AudioManager.Instance.StopAudio("run"); } //是否播放jump动画 if (m_verticalAccelerate > 0 && !m_isGround && !m_ani.GetBool("jump")) { m_ani.SetBool("jump", true); AudioManager.Instance.PlayAudio("jump"); } if (m_isGround) { m_ani.SetBool("jump", false); } if (m_verticalAccelerate < 0 && !m_isGround) { if (m_dir.x > 0) { m_body.transform.localEulerAngles = new Vector3(0, 90, 0); } else if (m_dir.x < 0) { m_body.transform.localEulerAngles = new Vector3(0, -90, 0); } } //检查当前格子是否有bra/underpant/sock物品 object[] p = new object[2]; p[0] = (object)Row; p[1] = (object)Column; if (curCellType == CELL_TYPE.BRA) { MapCell.ChangeToNone(false); GameStateManager.Instance().FSM.CurrentState.Message("GetBra", p); AudioManager.Instance.PlayAudio("get"); } else if (curCellType == CELL_TYPE.UNDERPANT) { MapCell.ChangeToNone(false); GameStateManager.Instance().FSM.CurrentState.Message("GetUnderpant", p); AudioManager.Instance.PlayAudio("get"); } else if (curCellType == CELL_TYPE.SOCKS) { MapCell.ChangeToNone(false); GameStateManager.Instance().FSM.CurrentState.Message("GetSocks", p); AudioManager.Instance.PlayAudio("get"); } //是否是出口 if (m_mapCtr.CanExit && Row == m_mapCtr.ExitEndRow && Column == m_mapCtr.ExitEndCol) { GameStateManager.Instance().FSM.CurrentState.Message("Vectory", p); } //是否遇到狗狗 if (m_mapCtr.IsMeetWithDog(transform.position)) { GameStateManager.Instance().FSM.CurrentState.Message("PlayerDead", p); } }
// Update is called once per frame void Update() { if (m_isJumping && !m_isMoving) { float step = m_moveSpeed * Time.deltaTime; Vector3 dir = jumpTarget.Position - transform.position; transform.position += dir * step; if (Vector3.Distance(transform.position, jumpTarget.Position) < step * 1.5f) { transform.position = new Vector3(jumpTarget.Position.x, jumpTarget.Position.y, transform.position.z); m_row = jumpTarget.CellData.row; m_col = jumpTarget.CellData.col; m_curCell = jumpTarget; m_isJumping = false; CheckGround(); State = PlayerState.STAND; if (null != m_playerMoveOverEvent) { m_playerMoveOverEvent.Invoke(this); } } } else if (m_isMoving) { float step = m_moveSpeed * Time.deltaTime; transform.position += m_dir * step; if (Vector2.Distance(new Vector2(transform.position.x, transform.position.y), new Vector2(m_targetPos.x, m_targetPos.y)) < step * 1.5f) { transform.position = new Vector3(m_targetPos.x, m_targetPos.y, transform.position.z); m_row += (int)m_dir.y; m_col += (int)m_dir.x; m_curCell = m_mapCtr.GetMapCell(m_row, m_col); m_isMoving = false; CheckGround(); State = PlayerState.STAND; if (null != m_playerMoveOverEvent) { m_playerMoveOverEvent.Invoke(this); } } } }