예제 #1
0
        public void Spawn(String nameObj)
        {
            if (!KRFacade.IsEmpty(_spawnPoint))
            {
                return;
            }

            if (_spawnableObjects.ContainsKey(nameObj))
            {
                GameObject kgo = Instantiate(_spawnableObjects[nameObj], _spawnPoint.ToVector3().Adjusted(), Quaternion.identity) as GameObject;

                kgo.transform.SetParent(_dynamics);

                kgo.GetComponent <KRTransform>().PlayerID = _krtransform.PlayerID;
                if (_krtransform.PlayerID == 0)
                {
                    kgo.transform.Find("EthanBody").GetComponent <Renderer>().sharedMaterial = _sweetBlue;
                    kgo.transform.Find("Spotlight").GetComponent <Light>().color             = new Color(0f, .4f, 1f);
                }
                else
                {
                    kgo.transform.Find("EthanBody").GetComponent <Renderer>().sharedMaterial = _sweetRed;
                    kgo.transform.Find("Spotlight").GetComponent <Light>().color             = new Color(.85f, .85f, .3f);
                }

                kgo.GetComponent <FiniteStateMachine>().Move(_rallyPoint);
            }
        }
예제 #2
0
 void OnDestroy()
 {
     if (OnDeath != null)
     {
         OnDeath(gameObject);
     }
     KRFacade.Remove(GetComponent <KRTransform>());
 }
예제 #3
0
        void Awake()
        {
            PlayerID = __playerID;
            Pos      = Vec2.FromVector3(transform.position);
            Size     = new Vec2(__sizeX, __sizeY);

            KRFacade.Add(this);
            Offer("OnBirth");
        }
        static void OnRightClick(Vector3 mousePosition)
        {
            if (!PlayerActions.IsMines())
            {
                return;
            }

            Vector3 worldPosition = new Vector3(-1, -1, -1);
            Vec2    pos           = new Vec2(-1, -1);

            Ray        ray = Camera.main.ScreenPointToRay(mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray.origin, ray.direction, out hit))
            {
                GameObject go = hit.collider.gameObject;
                if (go.CompareTag("Selectable"))
                {
                    worldPosition = go.transform.position;
                    pos           = Vec2.FromVector3(worldPosition);
                    if (!KRFacade.IsInBounds(pos))
                    {
                        return;
                    }
                    if (go.GetComponent <KRTransform>().PlayerID != NetworkAPI.PlayerId)
                    {
                        if (OnDemand != null)
                        {
                            OnDemand(new AttackAction(pos));
                            return;
                        }
                    }
                }
                else
                {
                    worldPosition = hit.point;
                    pos           = Vec2.FromVector3(worldPosition);
                    if (!KRFacade.IsInBounds(pos))
                    {
                        return;
                    }
                }
            }
            else
            {
                return;
            }

            if (OnDemand != null)
            {
                OnDemand(new MoveAction(pos));
            }
        }
예제 #5
0
        void OnModelDrag(int player, Vec2 beginModelPosition, Vec2 endModelPosition, Vec2 z)
        {
            DeselectUnits(player);

            IEnumerable <GameObject> gos = KRFacade.Find(beginModelPosition, endModelPosition, z);

            foreach (GameObject go in gos)
            {
                _selectedObjects[player].Add(go);
            }
            ApplySelection(player);
        }
예제 #6
0
        void OnModelSelect(int player, Vec2 modelPosition)
        {
            DeselectUnits(player);

            GameObject go = KRFacade.Find(modelPosition);

            if (go != null)
            {
                _selectedObjects[player].Add(go);
                ApplySelection(player);
            }
        }
예제 #7
0
        // TODO IDLE by type of units, here is the code for soldier. Replace 6 by vision sight
        public override Type Execute()
        {
            IEnumerable <GameObject> gameObjects = KRFacade.Around(_fsm.GetComponent <KRTransform>().Pos, 6);

            foreach (var obj in gameObjects)
            {
                if (obj.GetComponent <KRTransform>().PlayerID != _fsm.GetComponent <KRTransform>().PlayerID)
                {
                    _fsm.GetComponent <KRAttack>().Attack(obj);
                    return(typeof(AttackState));
                }
            }
            return(GetType());
        }
예제 #8
0
        public override Type Execute()
        {
            if (_attack.Target == null)
            {
                IEnumerable <GameObject> gameObjects = KRFacade.Around(_krtransform.Pos, 6);
                foreach (var obj in gameObjects)
                {
                    if (obj.GetComponent <KRTransform>().PlayerID != _krtransform.PlayerID)
                    {
                        _attack.Attack(obj);
                        return(null);
                    }
                }
                return(null);
            }

            if (_attack.Target.PlayerID == _krtransform.PlayerID)
            {
                _fsm.GetComponent <KRMovement>().Move(_attack.Target.Pos);
                return(typeof(MovementState));
            }

            if (Vec2.Dist(_attack.Target.Pos, _krtransform.Pos) ==
                _attack.Range)
            {
                _attack.UpdateGame();
            }
            else
            {
                if (_krmovement != null)
                {
                    if (_attack.Range == 1)
                    {
                        _krmovement.Follow(_attack.Target);
                        return(typeof(MovementState));
                    }
                    else
                    {
                        return(typeof(RangeState));
                    }
                }
                return(null);
            }

            return(GetType());
        }
예제 #9
0
 bool IsInRect(GameObject go)
 {
     return(new List <GameObject>(
                KRFacade.Find(
                    InputNetworkAdapter.BeginDrag,
                    Vec2.FromVector3(InputNetworkAdapter.WorldPosition(Input.mousePosition)),
                    Vec2.FromVector3(
                        InputNetworkAdapter.WorldPosition(
                            new Vector3(
                                Input.mousePosition.x,
                                Camera.main.WorldToScreenPoint(InputNetworkAdapter.BeginDrag.ToVector3()).y,
                                Input.mousePosition.z
                                )
                            )
                        )
                    )
                ).Contains(go));
 }
예제 #10
0
        public override Type Execute()
        {
            int dist = Mathf.Abs(Vec2.Dist(_krtransform.Pos, _attack.Target.Pos));

            if (dist > _attack.Range)
            {
                _krmovement.Move(_attack.Target.Pos);
                _krmovement.UpdateGame();
            }
            else if (dist == _attack.Range)
            {
                return(null);
            }
            else
            {
                Vec2 nearPos = KRFacade.NearSquareAt(_krtransform.Pos, _attack.Target.Pos, _attack.Range);
                if (nearPos != null)
                {
                    _krmovement.Move(nearPos);
                    return(typeof(MovementState));
                }
            }
            return(GetType());
        }
예제 #11
0
        public void UpdateGame()
        {
            if (Followee != null)
            {
                Target = Followee.Pos;
            }
            if (Target == null)
            {
                return;
            }
            if (_krtransform.Pos == Target)
            {
                Target   = null;
                Followee = null;                 // TEST
                return;
            }

            if (_currentFrame > 0)
            {
                --_currentFrame;
                return;
            }
            else
            {
                _currentFrame = Speed;
            }

            _waypoints = KRFacade.FindPath(_krtransform.Pos, Target);
            __waynodes.AddRange(_waypoints.Where(w => !__waynodes.Contains(w)));

            if (_waypoints == null)
            {
                Target = null;
            }
            else
            {
                QuadTreeNode <KRTransform> node;
                Vec2 nextPos = _krtransform.Pos;

                if (!_waypoints.Any())
                {
                    nextPos = Target;
                }
                else
                {
                    bool canTraceStraightLine = true;
                    do
                    {
                        node    = _waypoints.ElementAtOrDefault(1);
                        nextPos = (node == null) ? Target : node.Pos;
                        Bresenham.Line(_krtransform.Pos.X, _krtransform.Pos.Y, nextPos.X, nextPos.Y, delegate(int x, int y) {
                            if (!KRFacade.IsEmpty(new Vec2(x, y)))
                            {
                                canTraceStraightLine = false;
                                if (nextPos == Target)
                                {
                                    nextPos = _waypoints.First().Pos;
                                }
                                return(false);
                            }
                            return(true);
                        });
                        if (canTraceStraightLine)
                        {
                            if (nextPos != Target)
                            {
                                _waypoints = _waypoints.Skip(1);
                            }
                        }
                        else
                        {
                            node    = _waypoints.FirstOrDefault();
                            nextPos = (node == null) ? Target : node.Pos;
                        }
                    } while(canTraceStraightLine && nextPos != Target);
                }

                var lt = new List <Vec2>();

                Bresenham.Line(_krtransform.Pos.X, _krtransform.Pos.Y, nextPos.X, nextPos.Y, delegate(int x, int y) {
                    if (lt.Count == 0)
                    {
                        nextPos = new Vec2(x, y);
                    }
                    lt.Add(new Vec2(x, y));
                    return(true);
                });
                if (nextPos != _krtransform.Pos && KRFacade.IsEmpty(nextPos))
                {
                    KRFacade.Remove(GetComponent <KRTransform>());
                    _krtransform.Pos = nextPos;
                    KRFacade.Add(GetComponent <KRTransform>());
#if UNITY_EDITOR
                    __way.Add(_krtransform.Pos);
#endif
                }
                else
                {
                    _waypoints = null;
                    Target     = null;
                    Followee   = null;
                }
            }
        }
예제 #12
0
 public void Attack(Vec2 modelPoint)
 {
     BeforeAttack();
     Target = KRFacade.Find(modelPoint).GetComponent <KRTransform>();
     AfterAttack();
 }
예제 #13
0
 void UpdateGame()
 {
     KRFacade.UpdateGame();
 }