Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - silviufuicu

Pages: [1]
1
NGUI 3 Support / mobile stick movment to mouse click movment
« on: September 07, 2016, 12:12:14 AM »
Hello,
can you guys help me convert this script from (mobile)stick drag movment to mouse click (moba) movment ?

  1.  using UnityEngine;
  2.      using System;
  3.      using System.Collections;
  4.      using System.Collections.Generic;
  5.      using UICommon ;
  6.      using BlGame.GameEntity;
  7.      using BlGame.GameData;
  8.      using JT.FWW.GameData;
  9.      using BlGame.FSM;
  10.      using BlGame.GuideDate;
  11.      using BlGame.Model;
  12.      using BlGame.GameState;
  13.      
  14.      public class VirtualStickUI : MonoBehaviour
  15.      {
  16.          #region
  17.          public void SetVirtualStickUsable(bool enable)
  18.          {
  19.              canUse = enable;        
  20.              if (!enable)
  21.              {
  22.                  CloseStick();    
  23.              }
  24.          }
  25.      
  26.          #endregion
  27.      
  28.          #region
  29.          void Awake()
  30.          {
  31.              Init();
  32.          }
  33.      
  34.          void Init()
  35.          {
  36.              Instance = this;
  37.              orignalPos = transform.localPosition;
  38.              VirtualStickState = StickState.InActiveState;
  39.              point = transform.FindChild("stick");
  40.              btnSelf = transform.GetComponent<ButtonOnPress>();
  41.              
  42.              //firstInit = true;
  43.          }
  44.      
  45.      
  46.          void OnEnable()
  47.          {        
  48.              canUse = true;
  49.              SetVisiable(false);
  50.              btnSelf.AddListener(PressVirtual, ButtonOnPress.EventType.PressType);
  51.              BlGame.Ctrl.UIGuideCtrl.Instance.AddUiGuideEventBtn(btnSelf.gameObject);
  52.          }
  53.      
  54.          void OnDisable() {
  55.              btnSelf.RemoveListener(PressVirtual, ButtonOnPress.EventType.PressType);
  56.              VirtualStickState = StickState.InActiveState;
  57.          }
  58.      
  59.          
  60.          /// <param name="index">Index.</param>
  61.          /// <param name="isDown">If set to <c>true</c> is down.</param>
  62.          void PressVirtual(int ie,bool isDown)
  63.          {
  64.              if(isDown){
  65.                  ShowStick();
  66.              }else{
  67.                  CloseStick();
  68.              }
  69.          }
  70.      
  71.          void OnDrag(Vector2 pos)
  72.          {
  73.              if(canUse == false) return;
  74.      
  75.              Vector2 touchPos = UICamera.currentTouch.pos;
  76.              
  77.              SetPointPos(touchPos);
  78.      
  79.              VirtualStickState = StickState.MoveState;
  80.              SendMove();
  81.          }
  82.      
  83.    
  84.          void ShowStick()
  85.          {
  86.              if(canUse == false) return;
  87.              //PlayerManager.Instance.LocalPlayer.MoveCount = -1;
  88.              Vector2 touchPos = UICamera.currentTouch.pos;
  89.      
  90.              SetPointPos(touchPos);
  91.      
  92.              VirtualStickState = StickState.MoveState;
  93.              //SendMove();
  94.              transform.position = new Vector3(UICommonMethod.GetWorldPos(touchPos).x, UICommonMethod.GetWorldPos(touchPos).y, transform.position.z);
  95.              point.localPosition = Vector3.zero;
  96.              //VirtualStickState = StickState.ActiveState;
  97.              SetVisiable(true);        
  98.          }
  99.      
  100.          void CloseStick()
  101.          {
  102.              if (VirtualStickState == StickState.MoveState)
  103.              {
  104.                  SendStop();
  105.              }
  106.              Iselfplayer player = PlayerManager.Instance.LocalPlayer;
  107.              if (player != null && player.FSM != null && player.FSM.State == FsmState.FSM_STATE_ADMOVE)
  108.              {
  109.                  player.OnFSMStateChange(EntityFreeFSM.Instance);
  110.              }
  111.              SetVisiable(false);
  112.              VirtualStickState = StickState.InActiveState;
  113.              beforeDir = Vector3.zero;
  114.          }
  115.      
  116.          void SendStop(){
  117.              CGLCtrl_GameLogic.Instance.EmsgToss_AskStopMove ();
  118.          }
  119.          
  120.          private const float SendMoveInterVal = 0.05f;
  121.          private float MoveSendTime;
  122.          void SendMove()
  123.          {
  124.              Vector3 direction = GetPointerDirection();    
  125.              Transform target = JxBlGame.Instance.transform;
  126.              Entity entity = PlayerManager.Instance.LocalPlayer.RealEntity;
  127.              if (entity == null) {
  128.                  return ;        
  129.              }
  130.      
  131.              PlayState playState = GameStateManager.Instance.GetCurState() as PlayState;
  132.              if (playState == null)
  133.                  return;
  134.      
  135.      
  136.              target.position = entity.transform.position;
  137.              target.LookAt(entity.transform.position + direction);
  138.            
  139.              
  140.              Vector3 dir = new Vector3(0, 0, 0);
  141.      
  142.            
  143.              //target.transform.Rotate(new Vector3(0.0f, 45f, 0.0f));
  144.      
  145.            
  146.              if (playState.mCameraType == 1)      
  147.              {
  148.                  
  149.                  if (entity.CampType == EntityCampType.CampTypeA)
  150.                  {
  151.                      Quaternion rot = Quaternion.Euler(0, 45f, 0);
  152.                      dir = rot * target.forward;
  153.                  }
  154.                  else if (entity.CampType == EntityCampType.CampTypeB)
  155.                  {
  156.                      Quaternion rot = Quaternion.Euler(0, -135f, 0);
  157.                      dir = rot * target.forward;
  158.                  }
  159.                  else
  160.                  {
  161.                      Debug.Log("no valid entity camp type!");
  162.                  }
  163.              }
  164.              else                              
  165.              {
  166.            
  167.                  if (entity.CampType == EntityCampType.CampTypeA)
  168.                  {
  169.                      Quaternion rot = Quaternion.Euler(0, 0f, 0);
  170.                      dir = rot * target.forward;
  171.                  }
  172.                  else if (entity.CampType == EntityCampType.CampTypeB)
  173.                  {
  174.                      Quaternion rot = Quaternion.Euler(0, -180f, 0);
  175.                      dir = rot * target.forward;
  176.                  }
  177.                  else
  178.                  {
  179.                      Debug.Log("no valid entity camp type!");
  180.                  }
  181.              }
  182.                  
  183.              Vector3 dealPos = entity.transform.position + dir * Time.deltaTime * PlayerManager.Instance.LocalPlayer.EntityFSMMoveSpeed;
  184.              Vector3 dealPos1 = dealPos + dir * Time.deltaTime * PlayerManager.Instance.LocalPlayer.EntityFSMMoveSpeed * 2;      
  185.      
  186.      
  187.              if(dir != Vector3.zero && Time.time - MoveSendTime >= SendMoveInterVal)
  188.              {
  189.                  //if (SceneGuideTaskManager.Instance().IsNewsGuide() != SceneGuideTaskManager.SceneGuideType.NoGuide) {
  190.                  //    if (!PlayerManager.Instance.LocalPlayer.CheckGuideCanMove(dealPos1))
  191.                  //    {
  192.                  //        SendStop();
  193.                  //        return;
  194.                  //    }
  195.                  //}
  196.      
  197.                  MoveSendTime = Time.time;
  198.                  CGLCtrl_GameLogic.Instance.EmsgToss_AskMoveDir(dir);
  199.                  beforeDir = dir ;
  200.                  PlayerAdMove(dir);
  201.              }
  202.          }
  203.      
  204.          private void PlayerAdMove(Vector3 mvDir) {
  205.              Iselfplayer player = PlayerManager.Instance.LocalPlayer;
  206.              if (BlGame.Skill.BuffManager.Instance.isHaveStopBuff(player.GameObjGUID))
  207.              {
  208.                  return;
  209.              }
  210.              if (player.FSM == null)
  211.              {
  212.                  return;
  213.              }
  214.              if (player.FSM.State == FsmState.FSM_STATE_DEAD || player.FSM.State == FsmState.FSM_STATE_RUN || player.FSM.State == FsmState.FSM_STATE_FORCEMOVE
  215.                  || player.FSM.State == FsmState.FSM_STATE_RELIVE)
  216.              {
  217.                  return;
  218.              }
  219.              float mvSpeed = player.EntityFSMMoveSpeed;
  220.              if(mvSpeed <= 0)
  221.              {
  222.                  mvSpeed = 3.0f;
  223.              }
  224.              player.EntityFSMChangedata(player.realObject.transform.position, mvDir, mvSpeed);
  225.              player.OnFSMStateChange(PlayerAdMoveFSM.Instance);
  226.          }
  227.      
  228.          void SetVisiable(bool visiable)
  229.          {
  230.              if (visiable) {
  231.                  UICommonMethod.TweenColorBegin(gameObject, 0f, 1f);
  232.              }else {
  233.                  UICommonMethod.TweenColorBegin(gameObject, 0f, 0.5f);
  234.                  transform.localPosition = orignalPos;
  235.                  point.localPosition = Vector3.zero;
  236.              }
  237.          }
  238.      
  239.          void SetPointPos(Vector2 pos)
  240.          {
  241.              Vector3 newPos = UICommonMethod.GetWorldPos(pos)+new Vector3(0f,0f,point.position.z);
  242.              
  243.              if(Vector3.Distance(newPos,transform.position) > adjustRadius)
  244.              {        
  245.                  Vector3 direction = newPos - transform.position;
  246.                  direction.Normalize();
  247.                  direction *= adjustRadius;
  248.                  newPos = transform.position + direction;
  249.              }
  250.              point.position = newPos;        
  251.          }
  252.      
  253.          Vector3 GetPointerDirection()
  254.          {
  255.              Vector3 direction = point.position - transform.position;
  256.              
  257.              direction = new Vector3(direction.x, 0f, direction.y);
  258.              direction.Normalize();
  259.              
  260.              return direction;
  261.          }
  262.          
  263.          Vector3 NormalVector3(Vector3 pos)
  264.          {
  265.              float x = (float)Math.Round(pos.x,1);
  266.              float y = (float)Math.Round(pos.y,1);
  267.              float z = (float)Math.Round(pos.z,1);
  268.              Vector3 posTemp = new Vector3(x,y,z);
  269.              return posTemp;
  270.          }
  271.          
  272.          bool Vect3Compare(Vector3 pos1,Vector3 pos2)
  273.          {  
  274.              if(pos1.x != pos2.x){    
  275.                  return false ;
  276.              }
  277.              if(pos1.y != pos2.y){            
  278.                  return false ;
  279.              }
  280.              if(pos1.z != pos2.z){
  281.                  return false ;
  282.              }
  283.              
  284.              return true;
  285.          }
  286.      
  287.          #endregion
  288.      
  289.          #region    
  290.          public static VirtualStickUI Instance {
  291.              get;
  292.              private set;
  293.          }
  294.      
  295.      
  296.          public enum StickState
  297.          {
  298.              ActiveState,
  299.              MoveState,
  300.              InActiveState,
  301.          }
  302.      
  303.          public StickState VirtualStickState{
  304.              get;
  305.              private set;
  306.          }
  307.      
  308.          private Transform point;
  309.      
  310.          private ButtonOnPress btnSelf;
  311.      
  312.          private Vector3 orignalPos = new Vector3();
  313.      
  314.          private bool canUse = true;
  315.      
  316.          public float adjustRadius = 0.3f;
  317.      
  318.          private Vector3 beforeDir;
  319.          #endregion
  320.      }
  321.  
thanks.

Pages: [1]