Exemple #1
0
void CRole::damage(float damagePoint, CRole* attacker)
{
    float curHP = getCurHP();
    curHP = curHP - damagePoint;
    if (FLT_LE(curHP, 0.f))
    {
        setCurHP(0.f);
        FIGHT_RELATION->removeAllRelation(dynamic_cast<IFightingRelation*>(this));
        changeState(ROLE_STATE_DYING);
    }
    else
    {
        setCurHP(curHP);
        
        if (attacker)
        {
            if (getMovetarget().equals(Point(-1.f, -1.f)))
            {
                setMoveTarget(attacker->getLogicGrid()->getGridPos());
            }
        }
    }
    
    m_pHPBar->setPercentage(getCurHP() / getMaxHP());
    
    BF_MANAGER->wakeUpAllDefender();
}
void DetourCrowdComponent::executeEvent(GameEvent *event)
{
	switch (event->id())
	{
	case GameEvent::E_SET_PATH_GOAL:
		{
			const float * target = static_cast<float*>(event->data());
			setMoveTarget( target );
		}
		break;
	case GameEvent::E_STOP_MOVING:
		{
			const float* trans = GameEngine::getEntityTransformation( m_worldId );
			const float curPos[3] = {trans[12], trans[13], trans[14]};
			setMoveTarget( curPos );
		}
		break;
	}
}
Exemple #3
0
void
WorkerBee::onEnterState(State state)
{
    if (state == IN_HIVE) {
        setMoveState(MoveState::AT_REST);
    } else if (state == COLLECT_POLLEN) {
        setDebugStatus("collecting_pollen");
        setMoveState(MoveState::AT_REST);
    } else if (state == TO_FLOWER) {
        setMoveState(MoveState::TARGET);
    } else if (state == RETURN_HIVE) {
        setDebugStatus("back_to_hive");
        setMoveTarget(getHive().getPosition());
        setMoveState(MoveState::TARGET);
    }
}
void DetourCrowdComponent::addBackToSim()
{
	if( !m_isInCrowd && m_agentParams != 0 )
	{
		// Remove temp obstacle which was placed at the agent's position while being inactive.
		dtStatus removeStatus = GameEngine::dtRemoveTempObstacle( m_worldId, m_tempObsRef );
		if( removeStatus != DT_SUCCESS )
		{
			// Removing the obstacle is still in progress.
			m_pendingAddToSim = true;
			return;
		}

		// The obstacle was removed. The entity is now added back to the crowd simulation and given its new goal.
		m_pendingAddToSim = false;
		dtCrowd* crowd = DetourCrowdManager::instance()->getCrowd();
		const float* trans = GameEngine::getEntityTransformation( m_worldId );
		const float p[3] = { trans[12], trans[13], trans[14] };

		m_crowdId = crowd->addAgent( p, m_agentParams );
		if( m_crowdId == -1 )
		{
			GameLog::errorMessage( 
				"DetourCrowd: Could not add entity \"%s\" back to crowd after being temporarily removed",
				m_owner->id().c_str() );		
		}
		else
		{
			delete m_agentParams;
			m_agentParams = 0;
			
			m_isInCrowd = true;
			const float pos[3] = {m_targetPos[0], m_targetPos[1], m_targetPos[2]};
			setMoveTarget( pos );
			// Force crowd update to avoid a (0,0,0) rotation of the agent in the next
			// frame (default velocity of the agent when being added to the crowd).
			// Not pretty...
			crowd->update( 0.001, 0 );
		}
	}
}
Exemple #5
0
void
WorkerBee::onState(State state, sf::Time dt)
{
    Vec2d empty(-1.0, -1.0);

    // first state
    if (state == IN_HIVE) {
        // if bee has pollen transfer it to hive
        if (getPollen() > 0) {
            transferPollen(dt);
            flower_location_ = empty;
            setDebugStatus("in_hive_leaving_pollen");
        } else {
            // if bee has not enough energy to leave hive, eat its nectar
            if (getEnergy() < energy_leave_hive_
                && getHive().getNectar() > 0) {
                setDebugStatus("in_hive_eating");
                eatFromHive(dt);
            }
            // if there is a flower in memory and enough energy, target move
            // to this flower
            else if (flower_location_ != empty
                     && getEnergy() > energy_collect_pollen_) {
                setDebugStatus("in_hive_leaving");
                setMoveTarget(flower_location_);
                // change state to to flower
                nextState();
            } else {
                setDebugStatus("in_hive_no_flower");
            }
        }
    }

    // second state
    else if (state == TO_FLOWER) {
        setDebugStatus("to_flower");

        if (getEnergy() < energy_collect_pollen_) {
            nextState();
            nextState();
        }

        Flower* flower  = getAppEnv().getCollidingFlower(getVisionRange());

        if (flower) {
            setMoveTarget(flower->getPosition());
            setMoveState(MoveState::TARGET);
            if (isPointInside(flower->getPosition())) {
                nextState();
            }
        } else if (isPointInside(flower_location_)) {
            // go back to hive and clear location
            nextState();
            nextState();
            setFlowerLocation(Vec2d(-1,-1));
        }
    }

    // third state
    else if (state == COLLECT_POLLEN) {
        // if there is a flower at flower location and it has pollen and
        // bee has not enough pollen, eat pollen from flower
        Flower* flower(getAppEnv().getCollidingFlower(getCollider()));
        if ((getPollen() < max_pollen_)
            && (flower != nullptr)
            && (flower->getPollen() > 0)) {
            eatPollen(flower, dt);
        } else {
            // else skip collection
            nextState();
        }
    } else if (state == RETURN_HIVE) {
        // if bee is in hive change state to in hive
        if (getHive().isColliderInside(getCollider())) {
            nextState();
        }
    }
}
void DetourCrowdComponent::loadFromXml(const XMLNode* node)
{
	dtCrowdAgentParams ap;
	memset(&ap, 0, sizeof(ap));
	// The agent's radius affects the crowd calculations, but not the navmesh-based path plannning!
	ap.radius					= (float)atof( node->getAttribute( "radius", "0.4" ) );
	ap.height					= (float)atof( node->getAttribute( "height", "1.8" ) );
	ap.maxAcceleration			= (float)atof( node->getAttribute( "maxAcceleration", "4.0" ) );
	ap.maxSpeed					= (float)atof( node->getAttribute( "maxSpeed", "1.5" ) );
	ap.collisionQueryRange		= ap.radius * 12.0f;
	ap.pathOptimizationRange	= ap.radius * 30.0f;
	float targetX				= (float)atof( node->getAttribute( "targetX", "0.0" ) );
	float targetY				= (float)atof( node->getAttribute( "targetY", "0.0" ) );
	float targetZ				= (float)atof( node->getAttribute( "targetZ", "0.0" ) );
	m_yOffset					= (float)atof( node->getAttribute( "yOffset", "0.0" ) );
	m_targetRadius				= (float)atof( node->getAttribute( "targetRadius", "0.1" ) );

	// TODO!
	//if(  !(_stricmp(node->getAttribute("anticipateTurns", "0"), "false") == 0) 
	//	|| !(_stricmp(node->getAttribute("anticipateTurns", "0"), "0") == 0)
	//	)
	ap.updateFlags = 0; 
	if ( true /*m_toolParams.m_anticipateTurns*/)
		ap.updateFlags |= DT_CROWD_ANTICIPATE_TURNS;
	if ( true /*m_toolParams.m_optimizeVis*/)
		ap.updateFlags |= DT_CROWD_OPTIMIZE_VIS;
	if ( true/*m_toolParams.m_optimizeTopo*/)
		ap.updateFlags |= DT_CROWD_OPTIMIZE_TOPO;
	if ( true/*m_toolParams.m_obstacleAvoidance*/)
		ap.updateFlags |= DT_CROWD_OBSTACLE_AVOIDANCE;
	if ( true/*m_toolParams.m_separation*/)
		ap.updateFlags |= DT_CROWD_SEPARATION;

	// Avoidance types are defined in the manager.
	ap.obstacleAvoidanceType = (unsigned char)( (float)atof( node->getAttribute("avoidanceType", "3") ) );
	ap.separationWeight = (float)atof( node->getAttribute( "separationWeight", "0.5" ) );
	
	dtCrowd* crowd = DetourCrowdManager::instance()->getCrowd();
	
	// In case crowd init failed because of missing navmesh.
	if( crowd == 0 )
	{
		GameLog::warnMessage( "DetourCrowd: Could not add entity \"%s\" to crowd. Component removed!", m_owner->id().c_str() );		
		// Remove component
		GameEngine::setComponentData( m_worldId , "DetourCrowdComponent", 0 );
		return;
	}

	// Init agent
	const float* trans = GameEngine::getEntityTransformation( m_owner->worldId() );
	const float p[3] = {trans[12], trans[13], trans[14]};
	m_crowdId = crowd->addAgent(p, &ap);

	if (m_crowdId != -1)
	{
		m_isInCrowd = true;
		DetourCrowdManager::instance()->initAgentTrail( p, m_crowdId );
			
		if( targetX != 0.f || targetY != 0.f || targetY != 0.f) 
		{
			// A target the agent moves to was specified in the xml.
			float target[3] = { targetX, targetY, targetZ };
			setMoveTarget( target );
		}
		else
		{
			// Set target to current position.
			setMoveTarget( p ); // Needed to properly initialize the agent.
			tempRemoveFromSim();
		}
	}
	else // Couldn't add agent to crowd.
	{
		GameLog::warnMessage( "DetourCrowd: Could not add entity \"%s\" to crowd. Component removed! MAX_AGENTS = \"%d\" ",
			m_owner->id().c_str(), MAX_AGENTS );		
		// Remove component
		GameEngine::setComponentData( m_worldId , "DetourCrowdComponent", 0 );
	}
}
void Battlefield::setMoveTarget(int aIndex)
{
    setMoveTarget(grid_.hexFromAry(aIndex));
}