2022-07-20 17:04:21 +08:00
|
|
|
/*
|
|
|
|
zlib License
|
|
|
|
|
2023-02-14 20:05:31 +08:00
|
|
|
跳出一方天地 (C) 2022-2023 Lucas
|
2022-07-20 17:04:21 +08:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1.The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
2023-02-14 20:05:31 +08:00
|
|
|
|
|
|
|
using System;
|
2022-07-20 17:04:21 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2023-02-14 20:05:31 +08:00
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
using UnityEngine.UIElements;
|
2022-07-20 17:04:21 +08:00
|
|
|
|
|
|
|
public class MovementController : MonoBehaviour
|
|
|
|
{
|
2023-02-14 20:05:31 +08:00
|
|
|
public bool isJumping;
|
|
|
|
private bool isPressing;
|
|
|
|
private bool isDoubleJumping;
|
2022-07-20 17:04:21 +08:00
|
|
|
private Rigidbody2D rb2D;
|
|
|
|
public float xSpeed;
|
|
|
|
public float jumpSpeed;
|
2023-02-14 20:05:31 +08:00
|
|
|
private UIRect ui;
|
|
|
|
private struct UIRect
|
|
|
|
{
|
|
|
|
public Vector2 pos;
|
|
|
|
public float width;
|
|
|
|
public float height;
|
|
|
|
}
|
|
|
|
|
2022-07-20 17:04:21 +08:00
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
rb2D = GetComponent<Rigidbody2D>();
|
2023-02-14 20:05:31 +08:00
|
|
|
var speed = rb2D.velocity;
|
2022-07-20 17:04:21 +08:00
|
|
|
speed.x = xSpeed;
|
|
|
|
rb2D.velocity = speed;
|
2023-02-14 20:05:31 +08:00
|
|
|
ui = GetUiToScreenPos(GameObject.FindGameObjectWithTag("PauseButton").transform);
|
2022-07-20 17:04:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
2023-02-14 20:05:31 +08:00
|
|
|
var speed = rb2D.velocity;
|
2022-07-20 17:04:21 +08:00
|
|
|
speed.x = xSpeed;
|
2023-02-14 20:05:31 +08:00
|
|
|
if (Input.GetKey(KeyCode.Space) || IsTouched())
|
2022-07-20 17:04:21 +08:00
|
|
|
{
|
|
|
|
if (!isPressing)
|
|
|
|
{
|
|
|
|
isPressing = true;
|
|
|
|
if (isJumping && !isDoubleJumping)
|
|
|
|
{
|
|
|
|
speed.y = jumpSpeed;
|
|
|
|
isDoubleJumping = true;
|
|
|
|
}
|
|
|
|
else if (!isJumping || !isDoubleJumping)
|
|
|
|
{
|
|
|
|
speed.y = jumpSpeed;
|
|
|
|
isJumping = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
isPressing = false;
|
|
|
|
}
|
|
|
|
rb2D.velocity = speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnCollisionEnter2D(Collision2D collision)
|
|
|
|
{
|
2023-02-14 20:05:31 +08:00
|
|
|
if (!collision.gameObject.CompareTag("Floor")) return;
|
|
|
|
isJumping = false;
|
|
|
|
isDoubleJumping = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsTouched()
|
|
|
|
{
|
|
|
|
if (Input.touchCount == 0) return false;
|
|
|
|
for (var i = 0; i < Input.touchCount; ++i)
|
2022-07-20 17:04:21 +08:00
|
|
|
{
|
2023-02-14 20:05:31 +08:00
|
|
|
if (!IsTouchInUi(Input.GetTouch(i).rawPosition)) continue;
|
|
|
|
return false;
|
2022-07-20 17:04:21 +08:00
|
|
|
}
|
2023-02-14 20:05:31 +08:00
|
|
|
return true;
|
2022-07-20 17:04:21 +08:00
|
|
|
}
|
2023-02-14 20:05:31 +08:00
|
|
|
|
|
|
|
private UIRect GetUiToScreenPos(Transform trans)
|
|
|
|
{
|
|
|
|
var mapHeight = trans.GetComponent<RectTransform>().rect.height;
|
|
|
|
var mapWidth = trans.GetComponent<RectTransform>().rect.width;
|
|
|
|
Vector2 pos2D = trans.position;
|
|
|
|
pos2D.x -= mapWidth / 2;
|
|
|
|
pos2D.y -= mapHeight / 2;
|
|
|
|
return new UIRect
|
|
|
|
{
|
|
|
|
height = mapHeight,
|
|
|
|
width = mapWidth,
|
|
|
|
pos = pos2D
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsTouchInUi(Vector3 pos)
|
|
|
|
{
|
|
|
|
var isInRect = false;
|
|
|
|
var newPos = ui.pos;
|
|
|
|
if ((pos.x - 30) < (newPos.x + ui.width) && (pos.x + 30) > newPos.x &&
|
|
|
|
(pos.y - 30) < (newPos.y + ui.height) && (pos.y + 30) > newPos.y)
|
|
|
|
{
|
|
|
|
isInRect = true;
|
|
|
|
}
|
|
|
|
return isInRect;
|
|
|
|
}
|
|
|
|
|
2022-07-20 17:04:21 +08:00
|
|
|
}
|