Example #1
0
    // Update is called once per frame
    void Update()
    {
        if (_nextState != null && _currentState != null && _currentState.Status == StateStatus.Running)
        {
            Debug.LogWarning(_currentState.GetType() + " OnLeave");
            _currentState.OnLeave();
        }

        if (_currentState != null && _currentState.Status == StateStatus.LeaveEnded)
        {
            _currentState = null;
        }

        if (_currentState == null && _nextState != null)
        {
            _currentState = _nextState;
            _nextState    = null;
            Debug.LogWarning(_currentState.GetType() + " OnStart");
            _currentState.OnEnter();
        }

        if (_currentState != null && _currentState.Status == StateStatus.Running)
        {
            _currentState.Update();
        }
    }
Example #2
0
 public void OnEnemyDetected(GameObject player)
 {
     // If player is detected, we check if it is our teammate, or enemy, if it is enemy and we are not fleeing or attacking, we enter attacking state
     if (player.GetComponent <AbstractController>().team == team)
     {
         return;
     }
     if (!enemies.Contains(player))
     {
         enemies.Add(player);
         if (!(currentState.GetType() == typeof(FleeingState)) && !(currentState.GetType() == typeof(AttackingState)))
         {
             currentState = new AttackingState();
             currentState.OnStateEnter(this);
         }
     }
 }
Example #3
0
    public void ChangePrimaryState(AbstractState newState)
    {
        if (_currentState != null)
        {
            _currentState.OnStateExit();
        }

        _currentState = newState;

        var stateMachine = this;

        _currentState.OnStateEnter(ref stateMachine);

        OnStateChanged?.Invoke(_currentState.GetType());
    }
Example #4
0
        void Socket_ReadLine(object sender, ReadLineEventArgs e)
        {
            Packet packet     = e.packet;
            string methodName = PeerConst.GetCodeName(packet.Code);

            if (methodName == null)
            {
                return;
            }

            Type       type       = state.GetType();
            MethodInfo methodInfo = type.GetMethod(methodName);

            object[] args = { this, socket, packet };

            methodInfo.Invoke(state, args);
        }
Example #5
0
 public bool IsCurrentlyInState(Type state)
 {
     return(_currentState.GetType() == state);
 }
Example #6
0
        /**
         * The CheckCollision method contains code that handles collision with tiles and other entities.
         * This method can be overriden to extend the collision behaviour.
         *
         * @TODO: Simplify this. I am certain it should be possible.
         */
        public virtual void CheckCollision(AbstractState state)
        {
            if(state.GetType() == typeof(GameState) && this._collider) {
                Rectangle pRect = new Rectangle((int)Math.Floor(this.posVector.x), (int)Math.Floor(this.posVector.y), this.Sprite.Image.Width, this.Sprite.Image.Height);

                float x = this.posVector.x / TileHandler.TILE_WIDTH;
                float y = this.posVector.y / TileHandler.TILE_HEIGHT;

                //Window border collision
                if(this.movVector.y != 0) {
                    if(this.posVector.y <= 16) {
                        this.SetMovement(this.movVector.x, 0);
                        this.SetPosition(this.posVector.x, 16);
                    } else if(this.posVector.y >= state.Form.ClientSize.Height-this.Sprite.Image.Height-16) {
                        this.SetMovement(this.movVector.x, 0);
                        this.SetPosition(this.posVector.x, state.Form.ClientSize.Height-this.Sprite.Image.Height-16);
                    }
                }
                if(this.movVector.x != 0) {
                    if(this.posVector.x <= 16) {
                        this.SetMovement(0, this.movVector.y);
                        this.SetPosition(16, this.posVector.y);
                    } else if(this.posVector.x >= state.Form.ClientSize.Width-this.Sprite.Image.Width-16) {
                        this.SetMovement(0, this.movVector.y);
                        this.SetPosition(state.Form.ClientSize.Width-this.Sprite.Image.Width-16, this.posVector.y);
                    }
                }

                //Tile collision
                if(this.movVector.y < 0) {
                    Tile t = ((GameState)state).GetTileHandler().Map[(int)Math.Floor(x), (int)Math.Floor(y-0.5)];
                    if(t == null) t = ((GameState)state).GetTileHandler().Map[(int)Math.Ceiling(x), (int)Math.Floor(y-0.5)];
                    if (t != null) {
                        Rectangle tRect = new Rectangle((int)(t.Position.X * TileHandler.TILE_WIDTH), (int)(t.Position.Y * TileHandler.TILE_HEIGHT)+1, TileHandler.TILE_WIDTH, TileHandler.TILE_HEIGHT);

                        if(pRect.IntersectsWith(tRect)) {
                            this.SetMovement(this.movVector.x, 0);
                            this.SetPosition(this.posVector.x, (t.Position.Y+1) * TileHandler.TILE_HEIGHT);
                        }
                    }
                } else {
                    Tile t = ((GameState)state).GetTileHandler().Map[(int)Math.Floor(x), (int)Math.Floor(y+1)];
                    if(t == null) t = ((GameState)state).GetTileHandler().Map[(int)Math.Ceiling(x), (int)Math.Floor(y+1)];
                    if(t != null) {
                        Rectangle tRect = new Rectangle((int)(t.Position.X * TileHandler.TILE_WIDTH), (int)(t.Position.Y * TileHandler.TILE_HEIGHT)-1, TileHandler.TILE_WIDTH, TileHandler.TILE_HEIGHT);

                        if(pRect.IntersectsWith(tRect)) {
                            if(this._gravitate) this._airborne = false;
                            this.SetPosition(this.posVector.x, (t.Position.Y-1) * TileHandler.TILE_HEIGHT);
                        }
                    } else if(this._gravitate) {
                        this._airborne = true;
                    }
                }

                if(this.movVector.x < 0) {
                    Tile t = ((GameState)state).GetTileHandler().Map[(int)Math.Floor(x-0.5), (int)Math.Floor(y)];
                    if(t == null) t = ((GameState)state).GetTileHandler().Map[(int)Math.Floor(x-0.5), (int)Math.Ceiling(y)];
                    if(t != null) {
                        Rectangle tRect = new Rectangle((int)(t.Position.X * TileHandler.TILE_WIDTH)+1, (int)(t.Position.Y * TileHandler.TILE_HEIGHT), TileHandler.TILE_WIDTH, TileHandler.TILE_HEIGHT);

                        if(pRect.IntersectsWith(tRect)) {
                            this.SetPosition((t.Position.X+1) * TileHandler.TILE_WIDTH, this.posVector.y);
                        }
                    }
                } else if(this.movVector.x > 0) {
                    Tile t = ((GameState)state).GetTileHandler().Map[(int)Math.Floor(x+1), (int)Math.Floor(y)];
                    if(t == null) t = ((GameState)state).GetTileHandler().Map[(int)Math.Floor(x+1), (int)Math.Ceiling(y)];
                    if(t != null) {
                        Rectangle tRect = new Rectangle((int)(t.Position.X * TileHandler.TILE_WIDTH)-1, (int)(t.Position.Y * TileHandler.TILE_HEIGHT), TileHandler.TILE_WIDTH, TileHandler.TILE_HEIGHT);

                        if(pRect.IntersectsWith(tRect)) {
                            this.SetPosition((t.Position.X-1) * TileHandler.TILE_WIDTH, this.posVector.y);
                        }
                    }
                }
            }
        }