void WorldController::updateWorld(float dt){
	if(!isPause){
		for(int i=0; i < world->getSpirits().size(); i++){	
			if(world->getSpirits().at(i)->getState() == DEFENCE)
				world->getSpirits().at(i)->setDefence(isDefenceSpirit);
			world->getSpirits().at(i)->go(world->getBricks(), world->getPlayer(), world->getSpirits().at(0)->getPosition());
		}
	
		if(world->deadPlayer()) {
			onPause();
			respawn();
		}

		world->deadSpirit();
	
		if(world->isVictory()) {
			onPause();
			UserDefault::getInstance()->setIntegerForKey(LevelMenuScene::parseLevel(world->getCurrentLevel()).c_str(), record);
			UserDefault::getInstance()->setIntegerForKey(LevelMenuScene::parseLevel(world->getCurrentLevel() + 1).c_str(), 1);
			UserDefault::getInstance()->flush();
			Director::getInstance()->pushScene(WorldScene::create(LevelMenuScene::parseLevel(world->getCurrentLevel() + 1), world->getCurrentLevel() + 1, soundController_)->getScene());
			newWorld();
		}

		if(world->isGameOver()) {
			onPause();
			newWorld();
		}
		 
	}

}
Beispiel #2
0
void processDisconnect(ENetEvent& evt) {
	narf::console->println("Disconnected from " + narf::net::to_string(evt.peer->address));
	evt.peer->data = nullptr;
	connectState = ConnectState::Unconnected;

	// TODO: delete world and drop back to menu
	newWorld();
}
Beispiel #3
0
void processConnect(ENetEvent& evt) {
	connectState = ConnectState::Connected;
	narf::console->println("Connected to server " + narf::net::to_string(evt.peer->address));

	// reset the world
	newWorld();

	// TODO: put this somewhere common
	delete renderer;
	renderer = new narf::Renderer(world, *display, tilesTex);
}
Beispiel #4
0
void processWorldBalise(char* line, exo_content* ex)
{
  int decalage=0;
     while((line[decalage] == '\t' || line[decalage] == ' ') && line[decalage]!='\n')
	++decalage;
     if(line[decalage]=='\n')
   {
     printf("Erreur lors du décriptage de la balise world\n");
     return;
   }
   double x, y;
    sscanf(line+decalage, "%lf %lf", &x, &y);
    addWorldToExoContent(ex, newWorld(x,y));
}
void updateWorld2()
{
	//for(int n = 0; n < P.N; ++n)
	//{
	
	int size = world.size();
	std::vector<species> filler(size);	
	std::vector< std::vector< species > > newWorld(size,filler);

	std::vector<int> index = indices;

	std::random_shuffle(index.begin(), index.end());
	
	for(std::vector<int>::iterator i = index.begin(); i != index.end(); ++i)
	{
	  int y = (*i) % world.size();
	  int x = (int)( (*i) / world.size());
	
		//species at x,y dies and gets replaced
		//with prob 1-m it is from within the local community
		if(uniform() < P.m) //migration
		{
			species migrant = getSpeciesFromMetaCommunity();
			newWorld[x][y] = migrant;
		}
		else //no migration, local reproduction
		{
			int otherX = x; int otherY = y;
			if(P.dispersalType == "Square") findParent_Square(otherX,otherY,x,y); //square
			else if(P.dispersalType == "Circle") findParent_Circle(otherX,otherY,x,y); //circle
//			else if(P.dispersalType == "Reflect") findParent_Reflect(otherX,otherY,x,y);
			else findParent_Circle(otherX,otherY,x,y);  //default


			newWorld[x][y] = world[otherX][otherY];
			if(uniform() < P.speciationRate) newWorld[x][y] = newSpecies();
		}
	}

	world = newWorld;
}
Beispiel #6
0
void Display::loadWorld()
{
	stringstream filename;
	getSaveSuffix();
	filename <<"Save\\" << "World" << saveSuffix_ << ".dat";
	fstream file(filename.str());
	if (file.is_open() == false)
	{
		newWorld();
		saveWorld();
		isLoaded_ = true;
		return;
	}

	file.seekp(0, ios_base::beg);

	string text;
	file >> text;

	while (text != "End")
	{
		if (text == "Tile")
		{
			file.clear();
			Tile tile;
			tile.deserialize(file);
			m_map[tile.getPos()] = tile;
			file.clear();
		}
		if (text == "Player")
		{
			game::pHandler.getLocalPlayer().deserialize(file);
		}
		text = "";
		file.clear();
		file >> text;
	}

	reloadAll_ = true;
	isLoaded_ = true;
}
Beispiel #7
0
void Display::loadWorld(string filename)
{
    fstream file(filename);
	if (file.is_open() == false)
	{
		newWorld();
		saveWorld();
		isLoaded_ = true;
		return;
	}

	saveSuffix_=filename.substr(filename.find("d")+1, 2);

	file.seekp(0, ios_base::beg);

	string text;
	file >> text;

    while(text!="End")
    {
		if (text == "Tile")
		{
			file.clear();
			Tile tile;
			tile.deserialize(file);
			m_map[tile.getPos()] = tile;
			file.clear();
		}
		if (text == "Player")
		{
			game::pHandler.getLocalPlayer().deserialize(file);
			game::pHandler.addLocalPlayer(game::pHandler.getLocalPlayer());
		}
		text = "";
		file.clear();
		file >> text;
    }

	reloadAll_ = true;
	isLoaded_ = true;
}
Beispiel #8
0
void wdRegressionTest()
{
	printf("\n === Debut du test de regression de World === \n\n");
	World* W = newWorld(500.f, 500.f);

	Vertex* V1 = newVertex();
	Vertex* V2 = newVertex();
	Vertex* V3 = newVertex();

	Vertex* V10 = newVertex();
	Vertex* V11 = newVertex();
	Vertex* V12 = newVertex();
	Vertex* V13 = newVertex();

	vxSetPosition(V1, vec2(0.f, 0.f));
	vxSetPosition(V2, vec2(10.f, 0.f));
	vxSetPosition(V3, vec2(0.f, 10.f));

	vxSetPosition(V10, vec2(20.f, 0.f));
	vxSetPosition(V11, vec2(30.f, 0.f));
	vxSetPosition(V12, vec2(30.f, 10.f));
	vxSetPosition(V13, vec2(20.f, 10.f));

	Polygon* Poly = newPolygon(3, V1, V2, V3);
	Polygon* Rectangle = polyRectangle(V10, V11, V12, V13);

	wdAddVertex(W, V1);
	assert(wdGetVxCount(W) == 1);
	wdAddVertex(W, V2);
	assert(wdGetVxCount(W) == 2);
	wdAddVertex(W, V3);
	assert(wdGetVxCount(W) == 3);

	wdAddVertex(W, V10);
	assert(wdGetVxCount(W) == 4);
	wdAddVertex(W, V11);
	assert(wdGetVxCount(W) == 5);
	wdAddVertex(W, V12);
	assert(wdGetVxCount(W) == 6);
	wdAddVertex(W, V13);
	assert(wdGetVxCount(W) == 7);

	wdAddPolygon(W, Poly);
	assert(wdGetPolyCount(W) == 1);
	wdAddPolygon(W, Rectangle);
	assert(wdGetPolyCount(W) == 2);

	for(unsigned int i = 0; i < 10000; i++)
	{
		wdApplyForce(W, vec2(0.f, 0.6f));
		wdResolveVextex(W);
		wdUpdateGrid(W, FALSE);
		for(unsigned int j = 0; j < 4; j++)
		{
			wdResolveRigid(W);
			wdResolveElastic(W);
			wdHandleCollision(W);
		}
	}

	delWorld(W);
	printf("\n === Fin du test de regression de World ===== \n\n");
}
Beispiel #9
0
extern "C" int main(int argc, char **argv)
{
#if _WIN32
	// hack to redirect output to text files
	freopen("stdout.txt", "w", stdout);
	freopen("stderr.txt", "w", stderr);
#endif

	clientConsole = new narf::ClientConsole();
	narf::console = clientConsole;

	narf::console->println("Version: " + std::to_string(VERSION_MAJOR) + "." + std::to_string(VERSION_MINOR) + std::string(VERSION_RELEASE) + "+" VERSION_REV);

	if (enet_initialize() != 0) {
		narf::console->println("Error initializing ENet");
		return 1;
	}

	narf::cmd::cmds["set"] = cmdSet;
	narf::cmd::cmds["quit"] = cmdQuit;
	narf::cmd::cmds["connect"] = cmdConnect;
	narf::cmd::cmds["disconnect"] = cmdDisconnect;
	narf::cmd::cmds["save"] = cmdSave;
	narf::cmd::cmds["saveconfig"] = cmdSaveConfig;
	narf::cmd::cmds["load"] = cmdLoad;
	narf::cmd::cmds["stats"] = cmdStats;
	narf::cmd::cmds["about"] = cmdAbout;
	narf::cmd::cmds["music"] = cmdMusic;

	narf::MemoryFile iniMem;
	auto configFile = narf::util::appendPath(narf::util::userConfigDir("narfblock"), "client.ini");
	narf::console->println("Attempting to open user config file: " + configFile);
	if (!iniMem.read(configFile)) {
		configFile = narf::util::appendPath(narf::util::dataDir(), "client.ini");
		narf::console->println("Could not load user config file; falling back to local config file: " + configFile);
		if (!iniMem.read(configFile)) {
			narf::console->println("Could not load local config file; falling back to compile-time defaults");
		}
	}

	if (iniMem.size) {
		config.load(iniMem.data, iniMem.size);
	}

	config.updateSignal += configEvent;

	client = enet_host_create(nullptr, 1, narf::net::MAX_CHANNELS, 0, 0);
	if (!client) {
		fatalError("Could not create ENet client");
		return 1;
	}

	if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
		fatalError("SDL_Init() failed: " + std::string(SDL_GetError()));
		SDL_Quit();
		return 1;
	}

	musicMutex = SDL_CreateMutex();
	config.initBool("audio.enabled", true);

	SDL_DisplayMode mode;
	// TODO: iterate over monitors?
	if (SDL_GetDesktopDisplayMode(0, &mode)) {
		fatalError("SDL_GetDesktopDisplayMode failed: " + std::string(SDL_GetError()));
		SDL_Quit();
		return 1;
	}

	auto w = mode.w;
	auto h = mode.h;

	narf::console->println("Current video mode is " + std::to_string(w) + "x" + std::to_string(h));

	// TODO: convert these to be modifiable at runtime and use config.init*
	bool fullscreen = config.getBool("video.fullscreen", true);
	float width_cfg = config.getFloat("video.width", 0.6f);
	float height_cfg = config.getFloat("video.height", 0.6f);
	if (!fullscreen) {
		if (width_cfg > 1) {
			w = (int32_t)width_cfg;
		} else {
			w = (int32_t)((float)w * width_cfg);
		}
		if (height_cfg > 1) {
			h = (int32_t)height_cfg;
		} else {
			h = (int32_t)((float)h * height_cfg);
		}
		narf::console->println("Setting video to windowed " + std::to_string(w) + "x" + std::to_string(h));
	} else {
		narf::console->println("Setting video to fullscreen");
	}

	if (!initVideo(w, h, fullscreen)) {
		fatalError("Error: could not set OpenGL video mode " + std::to_string(w) + "x" + std::to_string(h));
		SDL_Quit();
		return 1;
	}

	clientConsole->setGLContext(display);

	config.initBool("video.vsync", false);

	newWorld();

	playerEID = world->entityManager.newEntity();
	{
		narf::EntityRef player(world->entityManager, playerEID);

		// initial player position
		player->position = narf::Vector3f(15.0f, 10.0f, 3.0f * 16.0f);
		player->prevPosition = player->position;
	}

	// initialize camera to look at origin
	cam.orientation.yaw = atan2f(cam.position.y, cam.position.x);
	cam.orientation.pitch = 0.0f;

	bouncyBlockEID = world->entityManager.newEntity();
	{
		narf::EntityRef bouncyBlock(world->entityManager, bouncyBlockEID);
		bouncyBlock->position = narf::Vector3f(10.0f, 10.0f, 21.0f);
		bouncyBlock->prevPosition = bouncyBlock->position;
		bouncyBlock->bouncy = true;
		bouncyBlock->model = true;
	}

	if (!init_textures()) {
		fatalError("init_textures() failed");
		return 1;
	}

	renderer = new narf::Renderer(world, *display, tilesTex);

	config.initInt32("video.renderDistance", 5);

	fpsTextBuffer = new narf::font::TextBuffer(*display, nullptr);
	blockInfoBuffer = new narf::font::TextBuffer(*display, nullptr);
	entityInfoBuffer = new narf::font::TextBuffer(*display, nullptr);
	locationBuffer = new narf::font::TextBuffer(*display, nullptr);

	config.initString("video.hudFont", "DroidSansMono");
	config.initInt32("video.hudFontSize", 30);
	if (!fpsTextBuffer->getFont()) {
		fatalError("Error: could not load HUD font");
		return 1;
	}

	config.initString("video.consoleFont", "DroidSansMono");
	config.initInt32("video.consoleFontSize", 18);
	if (!clientConsole->getFont()) {
		fatalError("Error: could not load Console font");
		return 1;
	}

	config.initString("video.consoleCursorShape", "default");

	config.initBool("video.stereo.enabled", false);
	config.initBool("video.stereo.cross", true);
	config.initFloat("video.stereo.separation", 0.1f);

	SDL_SetRelativeMouseMode(SDL_TRUE);

	config.initDouble("misc.tickRate", 60);
	config.initDouble("misc.maxFrameTime", 0.25);

	narf::console->println("Unicode test\xE2\x80\xBC pi: \xCF\x80 (2-byte sequence), square root: \xE2\x88\x9A (3-byte sequence), U+2070E: \xF0\xA0\x9C\x8E (4-byte sequence)");

	gameLoop = new ClientGameLoop(config.getDouble("misc.maxFrameTime"), config.getDouble("misc.tickRate"));
	gameLoop->run();

	if (connectState == ConnectState::Connected) {
		enet_peer_disconnect(server, (uint32_t)narf::net::DisconnectType::UserQuit);
	} else if (connectState == ConnectState::Connecting) {
		enet_peer_reset(server);
	}

	enet_host_flush(client);
	enet_host_destroy(client);
	enet_deinitialize();
	SDL_Quit();
	return 0;
}
Beispiel #10
0
	World * Physics::newWorld(float w, float h)
	{
		return newWorld(-w, -h, w, h, 0, 0, true);
	}