jump-out-of-the-world/Assets/Scripts/MovementController.cs

131 lines
3.7 KiB
C#

/*
zlib License
跳出一方天地 (C) 2022-2023 Lucas
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.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.UIElements;
public class MovementController : MonoBehaviour
{
public bool isJumping;
private bool isPressing;
private bool isDoubleJumping;
private Rigidbody2D rb2D;
public float xSpeed;
public float jumpSpeed;
private UIRect ui;
private struct UIRect
{
public Vector2 pos;
public float width;
public float height;
}
// Start is called before the first frame update
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
var speed = rb2D.velocity;
speed.x = xSpeed;
rb2D.velocity = speed;
ui = GetUiToScreenPos(GameObject.FindGameObjectWithTag("PauseButton").transform);
}
private void FixedUpdate()
{
var speed = rb2D.velocity;
speed.x = xSpeed;
if (Input.GetKey(KeyCode.Space) || IsTouched())
{
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)
{
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)
{
if (!IsTouchInUi(Input.GetTouch(i).rawPosition)) continue;
return false;
}
return true;
}
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;
}
}