Page 34 of 68

Re: Noob learns to code in 3 months

Posted: March 8th, 2017, 1:46 pm
by Yumtard
hmm managed to build now, nothing gets drawn and sreen size is different :S

Re: Noob learns to code in 3 months

Posted: March 8th, 2017, 2:06 pm
by albinopapa
That's no fun

Re: Noob learns to code in 3 months

Posted: March 8th, 2017, 4:07 pm
by Yumtard
yep,

tried doing the opposite too and copy all code from my game into the 3d repo but that too just gave it aids

Re: Noob learns to code in 3 months

Posted: March 8th, 2017, 7:40 pm
by Yumtard
Trying to figure out how the level thingy would be...
Is this in the ball park?

Code: Select all

const int width = 20;
const int height = 100;
const int cellDimension = 40;
int level[height][width];
ifstream read("level.txt");

//put the data from the text file into the 2d array
for (int i = 0; i <height; i++)
{
    for (int j = 0; j < width; j++)
    {
          read>>cells[i][j];
    }
}


// loop through the 2d array and choose action depending on number
for (int i = 0; i <height; i++)
    {
        for (int j = 0; j < width; j++)
        {
             switch(cells[i][j])
            {
             case 1:
                  MineManager.SpawnMine(j * cellDimension, i * cellDimension);
                  break;
             }
        }
    }


Re: Noob learns to code in 3 months

Posted: March 8th, 2017, 8:44 pm
by albinopapa
Yeah, that's practically it, just don't forget to call read.close() :).

Since you cache the file contents in the 2D array, you can close it once it's cached.

Re: Noob learns to code in 3 months

Posted: March 8th, 2017, 9:23 pm
by Yumtard
albinopapa wrote:Yeah, that's practically it, just don't forget to call read.close() :).

Since you cache the file contents in the 2D array, you can close it once it's cached.
Alright :) I'll try this, will have to rewrite all entities a bit first though

Re: Noob learns to code in 3 months

Posted: March 8th, 2017, 10:34 pm
by Yumtard
OMG!!!! So I was coding up a storm and through the whole thing felt 100% sure that lol no way this will work. But seems to kinda work :D

Feels good after spending 10+ hours failing today


edit: now one problem is that the first line in the text file will spawn objects at the top of the screen, the lines below will spawn objects below the top while I'd need them to be spawned above it.

like if I give the a mine the y position of i * cellDimension (say 40 * 40) this is 1600 and would mean it gets spawned below the ship.

Also it now stopped working out of nowhere :S no black holes show up anymore

Edit 2: figured out why no black holes were showing up but now the engine crashes instead lol

edit 3: found the bug

edit 4: still not sure how to make the map go "upwards"

last edit promise :D

........
I'm a poerplayer and a known liar/scumbag so here's edit 5:
figured it out: float(-i * cellDimension));
I tried this before but for whatever reason it didn't work. Well that's because I had a condition not to update the black hole if the position was less than 0

Re: Noob learns to code in 3 months

Posted: March 9th, 2017, 3:58 am
by LuisR14
tho the file just might autoclose if it goes out of scope o.o

Re: Noob learns to code in 3 months

Posted: March 9th, 2017, 12:37 pm
by Yumtard
Alright all the shits are now in this level thingy. Had to make a bunch of changes to all the entites and will have some cleaning up to do...

Not sure if this code is very good/efficient since all entities are created at the start and none of them are destroyed during the game. Will post code later if anyone want to look at it. Gonna figure out a way to reset the positions between lives now

Re: Noob learns to code in 3 months

Posted: March 9th, 2017, 1:15 pm
by Yumtard
Alright I'm just resetting the position between lives now, so that works.

So I call the level.Read() in the constructor of world.

The read function is like this

Code: Select all

void Level::ReadLevel()
{
	read.open("level.txt");
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			read >> level[i][j];
		}
	}
	read.close();

	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			switch (level[i][j])
			{
			case 1:
				bHoleM.SpawnBlackHole(float(j * cellWidth), float(i * cellHeight));
				break;

			case 2:
				droneM.SpawnDrone(float(j * cellWidth), float(i * cellHeight));
				break;

			case 3:
				eBoostM.SpawnEnergyBoost(float(j * cellWidth), float(i * cellHeight));
				break;

			case 4:
				mineM.SpawnMine(float(j * cellWidth), float(i * cellHeight));
				break;

			case 5:
				obstacleM.SpawnObstacle(float(j * cellWidth), float(i * cellHeight));
				break;

			case 6:
				shieldM.SpawnShield(float(j * cellWidth), float(i * cellHeight));
				break;

			case 7:
				smallEnemyM.SpawnSmallShip(float(j * cellWidth), float(i * cellHeight));
				break;

			case 8:
				bigEnemyM.Spawn(float(j * cellWidth), float(i * cellHeight));
				break;
			}
		}
	}
}
the spawnfunctions look like this

Code: Select all

void SmallEnemyManager::SpawnSmallShip(float X, float Y)
{
	nSmallShip++;
	smallShip.emplace_back<SmallEnemyShip>(SmallEnemyShip{ X, Y, smallExhaust, smallExplode, exploSound, leftM, rightM});
}
Then when I reset when the player dies, it's just pos = resetPos

So basically all entites gets created at the start of the game and exist through the whole game. Is this very inefficient and I should somehow deconstruct the entities when they go off screen etc or something similar?

If this seems alright I'm gonna make a function to read blackholelevel too and then start designing the level