188 lines
5.4 KiB
C#
188 lines
5.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Mirror;
|
|
using CustomExtensions;
|
|
using System.Threading.Tasks;
|
|
|
|
public class PickupItem : NetworkBehaviour
|
|
{
|
|
public PickupType type;
|
|
public float radius=1;
|
|
public Color gizmoColor = Color.green;
|
|
public bool active = true;
|
|
|
|
private Vector3 position;
|
|
public GameObject PickupEffect;
|
|
|
|
void Start(){
|
|
position = transform.position;
|
|
newPosition= transform.position;
|
|
movementTime = Random.Range(0.7f,1f);
|
|
|
|
if(isServer){
|
|
if(GetComponentInChildren<Animator>()!=null){GetComponentInChildren<Animator>().enabled = false;}
|
|
}
|
|
anim = GetComponentInChildren<Animator>();
|
|
}
|
|
|
|
Animator anim;
|
|
|
|
Vector3 newPosition;
|
|
float posTimer = 0;
|
|
float movementTime = 0;
|
|
float t2=100;
|
|
bool PlayerIsClose = false;
|
|
void Update()
|
|
{
|
|
// return;
|
|
if(!isServer){
|
|
if(t2 < movementTime * 2){
|
|
t2+=Time.deltaTime;
|
|
}else{
|
|
t2=0;
|
|
if(SceneData.localPlayer != null){
|
|
PlayerIsClose =(Vector3.Distance(transform.position, SceneData.localPlayer.transform.position) < 20);
|
|
if(anim!=null){ anim.enabled = PlayerIsClose;}
|
|
}
|
|
}
|
|
|
|
return;
|
|
if(!PlayerIsClose){return;}
|
|
// transform.position = Vector3.Lerp(transform.position, new Vector3(position.x + ((radius/2f) * Random.Range(-1f,1f)),position.y + ((radius/2f) * Random.Range(-1f,1f))),0.01f);
|
|
if(posTimer < movementTime){
|
|
posTimer+=Time.deltaTime;
|
|
}else{
|
|
newPosition = new Vector3(position.x + ((radius/2f) * Random.Range(-1f,1f)),position.y + ((radius/2f)));
|
|
|
|
posTimer= 0;
|
|
}
|
|
transform.position = Vector3.Lerp(transform.position, newPosition, 0.008f * movementTime);
|
|
return;
|
|
}
|
|
// if(!active){return;}
|
|
return;
|
|
Collider2D hit = Physics2D.OverlapCircle(transform.position, radius);
|
|
if(hit!=null ){
|
|
if(hit.GetComponent<SpaceshipController>()!=null){
|
|
hit.GetComponent<SpaceshipController>().CollectPickup(type);
|
|
active=false;
|
|
Deactivate(hit.transform);
|
|
}else if(hit.GetComponent<SpaceshipNetworkBot>()!=null){
|
|
hit.GetComponent<SpaceshipNetworkBot>().CollectPickup(type);
|
|
active=false;
|
|
Deactivate(hit.transform);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Reposition(Vector3 newPosition){
|
|
if(isServer){
|
|
reposition(newPosition);
|
|
|
|
RpcReposition(newPosition);
|
|
}else{
|
|
CmdReposition(newPosition);
|
|
}
|
|
}
|
|
|
|
[Command]
|
|
void CmdReposition(Vector3 newPosition){
|
|
reposition(newPosition);
|
|
|
|
RpcReposition(newPosition);
|
|
}
|
|
|
|
[ClientRpc]
|
|
void RpcReposition(Vector3 newPosition){
|
|
reposition(newPosition);
|
|
}
|
|
|
|
void reposition(Vector3 newPosition){
|
|
active=true;
|
|
transform.position = newPosition;
|
|
position= newPosition;
|
|
gameObject.SetActive(true);
|
|
// Debug.LogWarning("Repositoning pickup");
|
|
}
|
|
|
|
public void Deactivate(Transform playerPos){
|
|
if(isServer){
|
|
deactivate(playerPos);
|
|
RpcDeactivate(playerPos);
|
|
}else{
|
|
CmdDeactivate(playerPos);
|
|
}
|
|
}
|
|
|
|
void deactivate(Transform playerPos){
|
|
active=false;
|
|
// gameObject.SetActive(false);
|
|
if(false){
|
|
gameObject.SetActive(false);
|
|
SceneData.GameManager.DeactivatePickupItem(this);
|
|
NetworkServer.Destroy(gameObject);
|
|
|
|
}else{
|
|
MoveToPlayer(playerPos);
|
|
|
|
}
|
|
}
|
|
|
|
async void MoveToPlayer(Transform position){
|
|
float speed = 0.02f;
|
|
try{
|
|
while(transform != null && Vector3.Distance(position.position, transform.position) > 1f){
|
|
transform.position = Vector3.Lerp(transform.position, position.position, speed);
|
|
speed+= 0.02f;
|
|
// yield return new WaitForSeconds(0.01f);
|
|
await Task.Delay(10);
|
|
}
|
|
}catch{
|
|
Debug.Log("Target lost");
|
|
}
|
|
if(!isServer){
|
|
ParticleSystem particle = EffectPool.Spawn(PickupEffect, transform.position).GetComponent<ParticleSystem>();
|
|
particle.startColor = type.GetColor();
|
|
}
|
|
SceneData.GameManager.DeactivatePickupItem(this);
|
|
|
|
gameObject.SetActive(false);
|
|
if(isServer){
|
|
NetworkServer.Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
[Command]
|
|
void CmdDeactivate(Transform playerPos){
|
|
deactivate(playerPos);
|
|
RpcDeactivate(playerPos);
|
|
}
|
|
|
|
[ClientRpc]
|
|
void RpcDeactivate(Transform playerPos){
|
|
deactivate(playerPos);
|
|
}
|
|
|
|
private void OnDrawGizmos() {
|
|
if(!PlayerIsClose){return;}
|
|
Gizmos.color = gizmoColor;
|
|
Gizmos.DrawWireSphere(transform.position,radius);
|
|
}
|
|
|
|
public enum PickupType{
|
|
Star,
|
|
Star2,
|
|
Star3,
|
|
Star4,
|
|
Star5,
|
|
Moon1,
|
|
Moon2,
|
|
Moon3,
|
|
Moon4,
|
|
Moon5,
|
|
Twep
|
|
}
|
|
}
|
|
|