Ejemplo n.º 1
0
bool Map::checkSpawns() {
	for (unsigned int i = 0; i<this->getSpawnCount(); i++) {
		IFOSpawn* curSpawn = this->getSpawn(i);
		//As long as we didn't reach the allowed max, keep spawning monsters
		time_t lastCheckTime = time(nullptr) - curSpawn->getLastCheckTime();
		while (curSpawn->getCurrentlySpawned() < curSpawn->getMaxSimultanouslySpawned() && lastCheckTime >= curSpawn->getRespawnInterval()) {
			dword_t curSpawnId = curSpawn->getCurrentSpawnId();
			IFOSpawnEntry* selectedSpawn = nullptr;
			//Calculate the wanted spawnId and select the fitting spawnEntry
			if (curSpawnId >= curSpawn->getBasicMobSpawnCount()) {
				curSpawnId -= curSpawn->getBasicMobSpawnCount();
				selectedSpawn = &curSpawn->getTacticalMobSpawn(curSpawnId);
			}
			else {
				selectedSpawn = &curSpawn->getBasicMobSpawn(curSpawnId);
			}
			//Get the NPCData of the monster and spawn it
			//(also, add it to the map and thus visuality of the players)
			NPCData* npcData = mainServer->getNPCData(selectedSpawn->getMobId());
			for (unsigned int i = 0; i<selectedSpawn->getAmount(); i++) {
				position_t spawnPos(curSpawn->getPosition().x + QuickInfo::fRand(curSpawn->getAllowedSpawnDistance(), true),
					curSpawn->getPosition().y + QuickInfo::fRand(curSpawn->getAllowedSpawnDistance(), true));
				new Monster(npcData, mainServer->getAIData(selectedSpawn->getMobId()), this->getId(), spawnPos, curSpawn);
			}
			//Add the amount of spawned monsters to the total size of the spawn
			curSpawn->setCurrentlySpawned(curSpawn->getCurrentlySpawned() + selectedSpawn->getAmount());

			//Advance to the next spawnId; in case the max spawn was reached,
			//regress to the first basic spawn
			curSpawn->nextSpawnId();
		}
	}
	return true;
}
void ClientGameSession::AddDisc( unsigned char column, bool isPlayer0 )
{
    Vector2D spawnPos( 5.0f + column * 70.0f, 50.0f );
    Vector2D targetPos;

    for( int y = 0; y < ROW_COUNT; ++y )
    {
        if( mBoard[column][y] == BoardTile::BLANK )
        {
            targetPos = Vector2D( 5.0f + column * 70.0f, 105.0f + ( ROW_COUNT - y ) * 70.0f );
            break;
        }
    }

    Unit* discUnit;

    if( isPlayer0 )
    {
        discUnit = new Unit( spawnPos,
                             new Animation( mpTexture2DManager->getTexture( "Disc0" ), 1, Vector2D( 0, 0 ), 70, 70, 60, false ), Vector2D( 1, 1 ), "Disc0", false );
    }
    else
    {
        discUnit = new Unit( spawnPos,
                             new Animation( mpTexture2DManager->getTexture( "Disc1" ), 1, Vector2D( 0, 0 ), 70, 70, 60, false ), Vector2D( 1, 1 ), "Disc1", false );
    }

    mpUnitManager->addUnit( discUnit );

    //1 second lerp
    discUnit->LerpTo( targetPos, ( targetPos - discUnit->getPosition() ).getLength() * 1.0f );
}
void ClientGameObjectManagerAddon::createSpaceShip(int &threadOwnershipMask)
{

	//create hierarchy:
	//scene root
	//  scene node // tracks position/orientation
	//    SpaceShip

	//game object manager
	//  SpaceShipController
	//    scene node

	PE::Handle hMeshInstance("MeshInstance", sizeof(MeshInstance));
	MeshInstance *pMeshInstance = new(hMeshInstance) MeshInstance(*m_pContext, m_arena, hMeshInstance);

	pMeshInstance->addDefaultComponents();
	pMeshInstance->initFromFile("space_frigate_6.mesha", "FregateTest", threadOwnershipMask);

	// need to create a scene node for this mesh
	PE::Handle hSN("SCENE_NODE", sizeof(SceneNode));
	SceneNode *pSN = new(hSN) SceneNode(*m_pContext, m_arena, hSN);
	pSN->addDefaultComponents();

	Vector3 spawnPos(0, 0, 0.0f);
	pSN->m_base.setPos(spawnPos);

	pSN->addComponent(hMeshInstance);

	RootSceneNode::Instance()->addComponent(hSN);

	// now add game objects

	PE::Handle hSpaceShip("ClientSpaceShip", sizeof(ClientSpaceShip));
	ClientSpaceShip *pSpaceShip = new(hSpaceShip) ClientSpaceShip(*m_pContext, m_arena, hSpaceShip, 0.05f, spawnPos,  0.05f);
	pSpaceShip->addDefaultComponents();

	addComponent(hSpaceShip);

	// add the same scene node to tank controller
	static int alllowedEventsToPropagate[] = {0}; // we will pass empty array as allowed events to propagate so that when we add
	// scene node to the square controller, the square controller doesnt try to handle scene node's events
	// because scene node handles events through scene graph, and is child of space ship just for referencing purposes
	pSpaceShip->addComponent(hSN, &alllowedEventsToPropagate[0]);

	pSpaceShip->activate();
}
void ClientGameObjectManagerAddon::createTank(int index, int &threadOwnershipMask)
{

	//create hierarchy:
	//scene root
	//  scene node // tracks position/orientation
	//    Tank

	//game object manager
	//  TankController
	//    scene node
	
	PE::Handle hMeshInstance("MeshInstance", sizeof(MeshInstance));
	MeshInstance *pMeshInstance = new(hMeshInstance) MeshInstance(*m_pContext, m_arena, hMeshInstance);

	pMeshInstance->addDefaultComponents();
	pMeshInstance->initFromFile("kingtiger.x_main_mesh.mesha", "Default", threadOwnershipMask);

	// need to create a scene node for this mesh
	PE::Handle hSN("SCENE_NODE", sizeof(SceneNode));
	SceneNode *pSN = new(hSN) SceneNode(*m_pContext, m_arena, hSN);
	pSN->addDefaultComponents();

	Vector3 spawnPos(-36.0f + 6.0f * index, 0 , 21.0f);
	pSN->m_base.setPos(spawnPos);
	
	pSN->addComponent(hMeshInstance);

	RootSceneNode::Instance()->addComponent(hSN);

	// now add game objects

	PE::Handle hTankController("TankController", sizeof(TankController));
	TankController *pTankController = new(hTankController) TankController(*m_pContext, m_arena, hTankController, 0.05f, spawnPos,  0.05f);
	pTankController->addDefaultComponents();

	addComponent(hTankController);

	// add the same scene node to tank controller
	static int alllowedEventsToPropagate[] = {0}; // we will pass empty array as allowed events to propagate so that when we add
	// scene node to the square controller, the square controller doesnt try to handle scene node's events
	// because scene node handles events through scene graph, and is child of square controller just for referencing purposes
	pTankController->addComponent(hSN, &alllowedEventsToPropagate[0]);
}
    void LevelComponent::load()
    {
        gameplay::Properties * root = createProperties(_level.c_str());
        _width = root->getInt("width");
        _height = root->getInt("height");
        _tileWidth = root->getInt("tilewidth");
        _tileHeight = root->getInt("tileheight");

        int x = 0;
        int y = 0;

        gameplay::Properties * tileCollisionProperties = createProperties("res/physics/level.physics");
        gameplay::Properties * tileCollObjProperties = tileCollisionProperties->getNamespace("tile");
        gameplay::Properties * halfTileCollObjProperties = tileCollisionProperties->getNamespace("half_tile");
        gameplay::Properties * surfaceCollObjProperties = tileCollisionProperties->getNamespace("slope_45");
        gameplay::Properties * ladderCollObjProperties = tileCollisionProperties->getNamespace("ladder");
        gameplay::Properties * resetCollObjProperties = tileCollisionProperties->getNamespace("reset");
        std::map<int, TileType::Enum> tileIdTypes;

        if (gameplay::Properties * tilesetNamespace = root->getNamespace("tilesets_0", true))
        {
            if (gameplay::Properties * tilesetPropertiesNamespace = tilesetNamespace->getNamespace("tileproperties", true))
            {
                while (gameplay::Properties * tileTypeNamespace = tilesetPropertiesNamespace->getNextNamespace())
                {
                    int const tileId = atoi(tileTypeNamespace->getNamespace()) + 1;
                    TileType::Enum tileType = TileType::NONE;
                    char const * tileTypeName = tileTypeNamespace->getString("tile");

                    if(strcmp(tileTypeName, "BOX") == 0)
                    {
                        tileType = TileType::BOX;
                    }
                    else if(strcmp(tileTypeName, "SLOPE_45_L") == 0)
                    {
                        tileType = TileType::SLOPE_45_L;
                    }
                    else if(strcmp(tileTypeName, "SLOPE_45_R") == 0)
                    {
                        tileType = TileType::SLOPE_45_R;
                    }
                    else if(strcmp(tileTypeName, "LADDER") == 0)
                    {
                        tileType = TileType::LADDER;
                    }
                    else if(strcmp(tileTypeName, "RECTANGLE_TOP") == 0)
                    {
                        tileType = TileType::RECTANGLE_TOP;
                    }
                    else if (strcmp(tileTypeName, "RECTANGLE_BOTTOM") == 0)
                    {
                        tileType = TileType::RECTANGLE_BOTTOM;
                    }
                    else if (strcmp(tileTypeName, "RESET") == 0)
                    {
                        tileType = TileType::RESET;
                    }

                    bool const isValidTileType = tileType != TileType::NONE;

                    PLATFORMER_ASSERT(isValidTileType, "Invalid tile type defined for tile %d : '%s'", tileId, tileTypeName);

                    if(isValidTileType)
                    {
                        tileIdTypes[tileId] = tileType;
                    }
                }
            }
        }

        if (gameplay::Properties * layerNamespace = root->getNamespace("layers_0", true))
        {
            if (gameplay::Properties * dataNamespace = layerNamespace->getNextNamespace())
            {
                while (dataNamespace->getNextProperty())
                {
                    if (x == 0)
                    {
                        _grid.push_back(std::vector<Tile>());
                    }

                    int data = dataNamespace->getInt();
                    gameplay::Node * tileNode = nullptr;

                    if (data != EMPTY_TILE)
                    {
                        tileNode = gameplay::Node::create();
                        float const tileWidthScaled = _tileWidth * PLATFORMER_UNIT_SCALAR;
                        float const tileHeightScaled = _tileHeight * PLATFORMER_UNIT_SCALAR;
                        float const tilePosX = ((x * tileWidthScaled) + (tileWidthScaled / 2));
                        float const tilePosY = (-((y * tileHeightScaled) + (tileHeightScaled / 2)));
                        tileNode->translate(tilePosX, tilePosY, 0);
                        getParent()->getNode()->addChild(tileNode);

                        TileType::Enum tileType = TileType::NONE;
                        auto tileIdTypeItr = tileIdTypes.find(data);

                        if(tileIdTypeItr != tileIdTypes.end())
                        {
                            tileType = tileIdTypeItr->second;

                            switch(tileType)
                            {
                            case TileType::BOX:
                                tileNode->setCollisionObject(tileCollObjProperties);
                                break;
                            case TileType::SLOPE_45_L:
                                tileNode->rotateZ(MATH_DEG_TO_RAD(45));
                                tileNode->setCollisionObject(surfaceCollObjProperties);
                                break;
                            case TileType::SLOPE_45_R:
                                tileNode->rotateZ(MATH_DEG_TO_RAD(-45));
                                tileNode->setCollisionObject(surfaceCollObjProperties);
                                break;
                            case TileType::LADDER:
                                tileNode->translateY(tileHeightScaled / 2.0f);
                                tileNode->setCollisionObject(ladderCollObjProperties);
                                break;
                            case TileType::RECTANGLE_TOP:
                                tileNode->translateY(tileHeightScaled / 4.0f);
                                tileNode->setCollisionObject(halfTileCollObjProperties);
                                break;
                            case TileType::RECTANGLE_BOTTOM:
                                tileNode->translateY(-(tileHeightScaled / 4.0f));
                                tileNode->setCollisionObject(halfTileCollObjProperties);
                                break;
                            case TileType::RESET:
                                tileNode->setCollisionObject(resetCollObjProperties);
                                break;
                            default:
                                PLATFORMER_ASSERTFAIL("Unhandled tile type '%d'", tileType);
                            }
                        }

                        TerrainInfo * info = new TerrainInfo();
                        info->_tileType = tileType;
                        tileNode->setUserPointer(info);
                    }

                    _grid[y].emplace_back(Tile{ data, tileNode });
                    ++x;

                    if (x == _width)
                    {
                        x = 0;
                        ++y;

                        if (y == _height)
                        {
                            break;
                        }
                    }
                }
            }
        }

        if (gameplay::Properties * layerNamespace = root->getNamespace("layers_1", true))
        {
            if (gameplay::Properties * objectsNamespace = layerNamespace->getNextNamespace())
            {
                while (gameplay::Properties * objectNamespace = objectsNamespace->getNextNamespace())
                {
                    char const * gameObjectTypeName = objectNamespace->getString("name");
                    GameObject * gameObject = GameObjectController::getInstance().createGameObject(gameObjectTypeName, getParent());
                    gameplay::Vector2 spawnPos(objectNamespace->getInt("x"), -objectNamespace->getInt("y"));
                    spawnPos *= PLATFORMER_UNIT_SCALAR;

                    if(strcmp(gameObjectTypeName, "player") == 0)
                    {
                        _playerSpawnPosition = spawnPos;
                    }

                    std::vector<CollisionObjectComponent*> collisionComponents;
                    gameObject->getComponents(collisionComponents);

                    for(CollisionObjectComponent * collisionComponent : collisionComponents)
                    {
                        if(gameplay::PhysicsCharacter * character = collisionComponent->getNode()->getCollisionObject()->asCharacter())
                        {
                            if(character->isPhysicsEnabled())
                            {
                                collisionComponent->getNode()->setTranslation(spawnPos.x, spawnPos.y, 0);
                            }
                        }
                    }

                    _children.push_back(gameObject);
                }
            }
        }

        if (gameplay::Properties * propertiesNamespace = root->getNamespace("properties", true, false))
        {
            _texturePath = propertiesNamespace->getString("texture");
        }

        SAFE_DELETE(root);
        SAFE_DELETE(tileCollisionProperties);
    }