物理版打飛碟遊戲
這一次實現的是物理版的打飛碟遊戲,這一次和上一次的區別在於這一次增加了一個PhysicAction和PhysicsActionManager的類,PhysicAction專門來負責完成物理運動,Manager就專門來負責完成其管理了。第一步,首先給物體加上剛體的屬性。
第二步,我們增加PhysicAction,這一步其實和之前的CCAction沒有什麼區別,主要的差別就在於這裏不再確定地點了,畢竟我們負責把碟子丟出去,然後剩下的就依靠引力來解決這個問題了,代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhysicAction : SSAction
{
public Vector3 speed;
public static PhysicAction GetSSAction()
{
PhysicAction action = CreateInstance<PhysicAction>();
return action;
}
public override void Start(){
}
public override void Update(){
if (transform.position.y < -10 || transform.position.x <= -20 || transform.position.x >= 20){
gameObject.GetComponent<Rigidbody>().isKinematic = true;
gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
transform.position = Vector3.down;
callback.SSActionEvent(this);
}
}
}
第三步,我們增加PhysicsActionManager,這一步其實和之前的manager也相似,當然最大的區別還是在於剛體的區別,這裏我們設定飛碟的速度什麼的就和之前不一樣了。代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhysicsActionManager : SSActionManager, ISSActionCallback,IActionManager {
public FirstSceneController sceneController;
public List<PhysicAction> seq = new List<PhysicAction>();
public UserClickAction userClickAction;
public SkeetFactory skeets;
public void Start()
{
sceneController = (FirstSceneController)SSDirector.getInstance().currentSceneController;
sceneController.actionManager = this;
skeets = Singleton<SkeetFactory>.Instance;
}
public void PlaySkeet()
{
if(skeets.used.Count > 0)
{
GameObject skeet = skeets.used[0];
float x = Random.Range(-5, 5);
skeet.transform.GetComponent<Rigidbody>().isKinematic = false;
skeet.GetComponent<Rigidbody>().velocity = new Vector3(x, 8 * (Mathf.CeilToInt(FirstSceneController.times / 10) + 1) + 2, 6);
skeet.GetComponent<Rigidbody>().AddForce(new Vector3(0,8.8f, 0),ForceMode.Force);
PhysicAction physicAction = PhysicAction.GetSSAction();
seq.Add(physicAction);
this.RunAction(skeet, physicAction, this);
skeets.used.RemoveAt(0);
}
if (Input.GetMouseButtonDown(0) && sceneController.flag == 0)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitGameObject;
if (Physics.Raycast(ray, out hitGameObject))
{
GameObject gameObject = hitGameObject.collider.gameObject;
if (gameObject.tag == "skeet")
{
gameObject.transform.position = new Vector3(100,100,100);
userClickAction = UserClickAction.GetSSAction();
this.RunAction(gameObject, userClickAction, this);
}
}
}
base.Update();
}
public void SSActionEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null)
{
skeets.RecycleSkeet(source.gameObject);
seq.Remove(source as PhysicAction);
source.destory = true;
if (FirstSceneController.times >= 30)
sceneController.flag = 1;
}
public void CheckEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null)
{
}
public void Pause(){
if (sceneController.flag == 0){
foreach (var k in seq)
{
k.speed = k.transform.GetComponent<Rigidbody>().velocity;
k.transform.GetComponent<Rigidbody>().isKinematic = true;
}
sceneController.flag = 2;
}
else if (sceneController.flag == 2){
foreach (var k in seq)
{
k.transform.GetComponent<Rigidbody>().isKinematic = false;
k.transform.GetComponent<Rigidbody>().velocity = k.speed;
}
sceneController.flag = 0;
}
}
}
第四步,我們增加IActionManager接口,主要要求實現PlaySkeet和Pause,畢竟這一個和之前的都不太一樣。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public interface IActionManager {
void PlaySkeet();
void Pause();
}
至此完成。