Пример #1
0
/**
 * @inheritDoc
 */
void PlayerInputObject::Update(f32 DeltaTime) {
    ASSERT(m_bInitialized);

    u32 mModified = 0;
    
    if (m_upInputAction->hasChanged()) {
        mModified |= System::Changes::Input::Velocity;
        m_velocity.y += m_upInputAction->isActive() ? -1 : 1;
    }
    if (m_rightInputAction->hasChanged()) {
        mModified |= System::Changes::Input::Velocity;
        m_velocity.x += m_rightInputAction->isActive() ? 1 : -1;
    }
    if (m_downInputAction->hasChanged()) {
        mModified |= System::Changes::Input::Velocity;
        m_velocity.y += m_downInputAction->isActive() ? 1 : -1;
    }
    if (m_leftInputAction->hasChanged()) {
        mModified |= System::Changes::Input::Velocity;
        m_velocity.x += m_leftInputAction->isActive() ? -1 : 1;
    }
    if (m_rightRotateInputAction->hasChanged()) {
        mModified |= System::Changes::Input::Rotation;
        m_rotation.z += m_rightRotateInputAction->isActive() ? -1 : 1;
    }
    if (m_leftRotateInputAction->hasChanged()) {
        mModified |= System::Changes::Input::Rotation;
        m_rotation.z += m_leftRotateInputAction->isActive() ? 1 : -1;
    }
    if (m_shotInputAction->hasChanged()) {
        if (m_shotInputAction->isActive()) {
            createShot();
        }
        mModified |= System::Changes::Input::Action;
        m_shotKeyboardButtonData->down = m_shotInputAction->isActive();
    }
    
    if (mModified != 0) {
        PostChanges(mModified);
    }
}
Пример #2
0
	void Player::logic(Room *room)
	{
		//std::cout << "HP:" << getHealth() << " MP:" << getMana() << "/" << getMaximumMana() << " Couter:" << mRegenerateManaCounter << std::endl;
		mStateCounter++;		

		if (mInvincibleCounter > 0) {
			mInvincibleCounter--;
		}

		if (getState() == DEAD) {
			return;
		}

		regenerateMana();
		
		const KeyState &keys = room->getKeyState();
		TileMap *tileMap = room->getTileMap();

		bool onIce = isOnIce(tileMap);

		int acceleration = AIR_ACCELERATION;
		if (getState() == GROUND) {
			if (onIce) {
				acceleration = ICE_ACCELERATION;
			} else {
				acceleration = GROUND_ACCELERATION;
			}
		}

		if (keys.isLeftHeld() && !keys.isRightHeld() && !mHurt) {
			mDX -= acceleration;
			if (mDX < -RUN_SPEED) {
				mDX = -RUN_SPEED;
			}
			mRunFrame += onIce && mDX > -20 ? 3 : 2;
			mFacingLeft = true;
			mRunning = true;
		} else if (!keys.isLeftHeld() && keys.isRightHeld() && !mHurt) {
			mDX += acceleration;
			if (mDX > RUN_SPEED) {
				mDX = RUN_SPEED;
			}
			mRunFrame += onIce && mDX < 20 ? 3 : 2;
			mFacingLeft = false;
			mRunning = true;
		} else if (mDX > 0 && !mHurt) {
			mDX -= onIce ? 1 : 4;
			if (mDX < 0) {
				mDX = 0;
			}
			mRunning = false;
		} else if (mDX < 0 && !mHurt) {
			mDX += onIce ? 1 : 4;
			if (mDX > 0) {
				mDX = 0;
			}
			mRunning = false;
		} else {
			mRunning = false;
		}

		// Crouching
		if (!mRunning && keys.isDownHeld() && getState() == GROUND) {
			if (!mCrouching) {
				mCrouching = true;
				mY += CROUCH_SHRINK;
				mH -= CROUCH_SHRINK;
			}
		} else if (mCrouching) {
			mCrouching = false;
			mY -= CROUCH_SHRINK;
			mH += CROUCH_SHRINK;
		}
		

		if (mDX == 0 || getState() != GROUND) {
			mRunFrame = 0;
		}

		// Interrupting jumping
		if (!keys.isJumpHeld() && getState() == AIR_UP && mDY > JUMP_CONTROL && !mHurt) {
			mDY = JUMP_CONTROL;
		}

		if (!keys.isJumpHeld() && (getState() == AIR_UP || getState() == AIR_DOWN)) {
			mJumpKeyHeld = false;
		}

		// Jumping
		if (keys.isJumpPressed() && getState() == GROUND && !keys.isDownHeld()) {
			mDY = JUMP_STRENGTH;
			setState(AIR_UP);
			mJumpKeyHeld = true;
			mJumpY = getCenterY();
		}
		// Drop off platform
		if (keys.isJumpPressed() && mOnPlatform && keys.isDownHeld()) {
			mDY = 1;
			mY++;
			setState(AIR_DOWN);
			mJumpKeyHeld = true;
			mJumpY = getCenterY();
		}

		// Sideways movement
		if (onIce) {
			if (mDX < 0) {
				mX += (mDX - 7) / 8;
			} else {
				mX += (mDX + 7) / 8;
			}
		} else {
			mX += mDX / 8;
		}
		

		// Left Wall
        int solidLeftOffset;
		if (mDX < 0 && solidLeft(room, solidLeftOffset)) 
        {
			// Align player to solid object
			mX += solidLeftOffset;
            
            mDX = 0;
		}

		// Right Wall
        int solidRightOffset;
		if (mDX > 0 && solidRight(room, solidRightOffset)) 
        {
			// Align player to solid object
			mX -= solidRightOffset;
			mDX = 0;
		}

		// Vertical movement
		if (getState() == AIR_DOWN) {
			mY += mDY > MAX_AIR_SPEED ? MAX_AIR_SPEED : mDY;
		} else if (getState() == AIR_UP) {
			mY -= mDY > MAX_AIR_SPEED ? MAX_AIR_SPEED : mDY;
		}

		// Falling, acceleration
		if (getState() == AIR_DOWN) {
			mDY += 1;
		}

		// Raising, deacceleration
		if (getState() == AIR_UP) {
			mDY--;
			if (mDY < 0) {
				setState(AIR_DOWN);
				mDY = 0;
			}
		}

		// Ceiling bouncing
        int solidAboveOffset;
		if (getState() == AIR_UP && solidAbove(room, solidAboveOffset)) 
        {
			setState(AIR_DOWN);
			mDY = 0;
			// Align player to solid object
			mY += solidAboveOffset;
		}

		// Ground bouncing / landing
        int groundOffset;
		if (getState() == AIR_DOWN && isOnGround(room, groundOffset)) {
			if (keys.isJumpHeld() && !mJumpKeyHeld && !keys.isDownHeld()) {
				// Rejump
				mDY = JUMP_STRENGTH;
				setState(AIR_UP);
				mJumpKeyHeld = true;
			} else {
				// Land
				setState(GROUND);
				mDY = 0;				
			}
			mHurt = false;

			// Align to tile
			mY -= groundOffset;
			mJumpY = getCenterY();
		}

		// Falling
        int fallingOffset;
		if (getState() == GROUND && !isOnGround(room, fallingOffset)) {
			setState(AIR_DOWN);
			mDY = 0;
		}

		// Shooting
		if (keys.isFirePressed())	{
			if (mShotCounter >= SHOT_DELAY) {
				mShotCounter = 0;
				room->addEntity(createShot());
				mShootAfterDelay = false;
			} else {
				mShootAfterDelay = true;
			}
		}
		if (mShotCounter >= SHOT_DELAY && mShootAfterDelay) {
			mShotCounter = 0;
			room->addEntity(createShot());
			mShootAfterDelay = false;
		}
		
		mShotCounter++;


		if (keys.isSpecialPressed()) 
        {
		    if (mShotType == NORMAL)
            {
                mShotType = ICE;
            }
            else if (mShotType == ICE)
            {
                mShotType = FIRE;
            }
            else
            {
                mShotType = NORMAL;
            }
        }
	}