Game Logic

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 5229 Views - Last Post: 09 January 2013 - 02:55 PM Rate Topic: -----

#16 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: Game Logic

Posted 23 December 2012 - 02:31 PM

View PostSilver1992, on 23 December 2012 - 08:52 PM, said:

But how do I connect the correct attribute with the right tile?


Have you started programming it? A lot of it is figuring out how you want to organize your game.
Was This Post Helpful? 0
  • +
  • -

#17 Silver1992  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 13-August 12

Re: Game Logic

Posted 24 December 2012 - 10:58 AM

To ButchDean

Yes, what is explained there is a function that returns a tile type. An attribute normally refers to something characteristic for the object in question. In this scenario that would be that would some basic bool variables like "dense, walkAble, swimAble" or something like that.

With a tile class of some sort I could just make data members that would be associated with each type of tile. If I understand what BlueMelon wrote correctly using this method I would have to use some kind of Switch statement in my players movement code. Something like if the Map.tiletype(x,y) return 1 then the player can move. 2 is water and if the player waterWalk is false then he can't move there and with 3 he cant move at all.

Or?

I've made some very basic stuff so far. Most of what I'm doing right now is making small components then trying to put them together.

My next "project" is to write myself a Map class which handles everything about the map.
Was This Post Helpful? 0
  • +
  • -

#18 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Game Logic

Posted 24 December 2012 - 12:28 PM

Post some code and we'll take it from there. You're thinking and not doing when you should be doing both.

This post has been edited by ButchDean: 24 December 2012 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#19 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Re: Game Logic

Posted 24 December 2012 - 12:42 PM

As ButchDean said, the next best step for you is to show us some code. I don't know how extensive your knowledge is of C++. Personally, i would start by creating a struct data structure named "Tile" that holds all the attributes you want a tile to have, then make a class data structure named "tileClass" that holds x number of tiles, then you can begin making functions like "Map.tiletype(x,y)" that return information about the tiles.
Was This Post Helpful? 0
  • +
  • -

#20 Silver1992  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 13-August 12

Re: Game Logic

Posted 24 December 2012 - 01:36 PM

Will do. Going to add what I have tomorrow. Thanks for the help so far!
Was This Post Helpful? 0
  • +
  • -

#21 Silver1992  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 13-August 12

Re: Game Logic

Posted 27 December 2012 - 04:29 PM

Ok sorry. Due to Christmas my plans are somewhat set back. Still working on it!
Was This Post Helpful? 0
  • +
  • -

#22 Silver1992  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 13-August 12

Re: Game Logic

Posted 05 January 2013 - 09:31 AM

Ok, one quick question. Should not being able to walk on water be a collision detection or some other game mechanic?
Was This Post Helpful? 0
  • +
  • -

#23 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Game Logic

Posted 05 January 2013 - 09:46 AM

Collision detection tells you whether you come into contact with something in the game world or not. That should tell you what to do.
Was This Post Helpful? 0
  • +
  • -

#24 Silver1992  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 13-August 12

Re: Game Logic

Posted 05 January 2013 - 09:50 AM

Going with Collision detection for it then.
Was This Post Helpful? 0
  • +
  • -

#25 Silver1992  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 13-August 12

Re: Game Logic

Posted 06 January 2013 - 11:39 AM

View PostJava Student, on 24 December 2012 - 12:42 PM, said:

As ButchDean said, the next best step for you is to show us some code. I don't know how extensive your knowledge is of C++. Personally, i would start by creating a struct data structure named "Tile" that holds all the attributes you want a tile to have, then make a class data structure named "tileClass" that holds x number of tiles, then you can begin making functions like "Map.tiletype(x,y)" that return information about the tiles.


Could you elaborate on this idea a bit more?

This post has been edited by Silver1992: 06 January 2013 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

#26 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Re: Game Logic

Posted 06 January 2013 - 12:20 PM

View PostSilver1992, on 06 January 2013 - 11:39 AM, said:

Could you elaborate on this idea a bit more?


Im not quite sure how to elaborate anymore without explicitly giving you the code.

If you want to write a tileClass like i suggested then you can create one yourself, and post back here if you have any problems with such as a specific function or etc...

*A tip for collision detection, you should never be standing on a water tile and be asking yourself "Should i be standing on this tile?" Why? Because you should ask yourself this BEFORE you step onto the next tile.
ie:
if(getNextTile()==validTile){
     movePlayer();
}


Was This Post Helpful? 0
  • +
  • -

#27 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Game Logic

Posted 06 January 2013 - 12:40 PM

About the water tile, you might want something to happen if the player 'collides' with it; will they drown, would the character animation switch to a swimming animation? :)
Was This Post Helpful? 1
  • +
  • -

#28 Silver1992  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 13-August 12

Re: Game Logic

Posted 06 January 2013 - 02:45 PM

Ok, so I've written something very basic. It's my current Map class. Nothing special, just the framework.

map.h
#ifndef _MAP_H            // Prevent multiple definitions if this 
#define _MAP_H            // file is included in more than one place
#define WIN32_LEAN_AND_MEAN

#include <string>
#include "game.h"
#include "textureManager.h"
#include "image.h"
#include "player.h"
#include <fstream>
#include <iostream>
#include "constants.h"

namespace mapNS
{
	    const int TEXTURE_SIZE = 32; 
        const int TEXTURE_COLS = 4;
}


class Map
{

private:
	TextureManager mapTextures;									 // This is the texture which holds the tiles. Our texture holds 8 textures at the moment

	Image mapTiles;												 // Holds the information of the specific 32x32 rectangle of the texture we're going to draw.

	int mapArray[ TILE_MAP_HEIGHT ] [ TILE_MAP_WIDTH ];         // Each Array will hold an array that == which tile it represents.

	float   mapX, mapY;											// To scroll the game around

public:
	// Constructor
	Map();
	// Destructor
    ~Map();

	// Reads the map into the array and initializes the needed graphics	
	void initialize(Graphics *graphics);

	// The game has to be updated on what to draw based upon input
	// frameTime = The timezies
	// Input = So we can obtain input
	// speed = Speed of the player
	void update(float frameTime, Input *input, float speed);

	//Drawing of map
	void draw();

	// Read the mapfile into the mapArray
	void readMap();
};

#endif



map.cpp
#include "map.h"
using namespace mapNS;

//=============================================================================
// Constructor
//=============================================================================
Map::Map()
{
	mapX = 0;
    mapY = 0;

}


Map::~Map(void)
{
}

void Map::initialize(Graphics *graphics)
{
	readMap();

	// Tries to initialize the Texture for the Map
	if (!mapTextures.initialize(graphics,MAP_TILES))
        throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing tile textures"));

	// Here we try to initialize the image that we'll use to draw the shabang
    if (!mapTiles.initialize(graphics,TEXTURE_SIZE,TEXTURE_SIZE,TEXTURE_COLS,&mapTextures))
        throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing tile"));
    mapTiles.setFrames(0, 0);
    mapTiles.setCurrentFrame(0);

}


void Map::update(float frameTime, Input *input, float speed)
{
	if (input->isKeyDown(LEFT_KEY))     // if left
    {   
        mapX += speed * frameTime;
    }
    if (input->isKeyDown(RIGHT_KEY))    // if right
    {
        mapX -= speed * frameTime;
    }
	
    if (input->isKeyDown(UP_KEY))       // if up arrow
    {
        mapY += speed * frameTime;
    }
    if (input->isKeyDown(DOWN_KEY))     // if down arrow
    {
        mapY -= speed * frameTime;
    }



	//
	// Check if the map is out of bounds
	//

	    if(mapX > 0)    // if map past left edge
    {
        mapX = 0;   // stop at left edge of map
    }
    // if map past right edge
    else if(mapX < (-TILE_MAP_WIDTH * TEXTURE_SIZE) + GAME_WIDTH)
    {
        // stop at right edge of map
        mapX = (-TILE_MAP_WIDTH * TEXTURE_SIZE) + GAME_WIDTH;
    }
    if(mapY > 0)    // if map past top edge
    {
        mapY = 0;   // stop at top edge of map  
    }
    // if map past bottom edge
    else if(mapY < (-TILE_MAP_HEIGHT * TEXTURE_SIZE) + GAME_HEIGHT)
    {
        // stop at bottom edge of map
        mapY = (-TILE_MAP_HEIGHT * TEXTURE_SIZE) + GAME_HEIGHT;
    }
}

void Map::draw()
{
	for(int row=0; row<TILE_MAP_HEIGHT; row++)       // for each row of map
    {
        mapTiles.setY( (float)(row*TEXTURE_SIZE) ); // set tile Y
        for(int col=0; col<TILE_MAP_WIDTH; col++)    // for each column of map
        {
                      // if tile present
            
                mapTiles.setCurrentFrame(mapArray[row][col] - 1);    // set tile texture
                mapTiles.setX( (float)(col*TEXTURE_SIZE) + mapX );  // set tile X
                mapTiles.setY( (float)(row*TEXTURE_SIZE) + mapY );  // set tile Y
                // if tile on screen
                if( (mapTiles.getX() > -TEXTURE_SIZE && mapTiles.getX() < GAME_WIDTH) &&
                    (mapTiles.getY() > -TEXTURE_SIZE && mapTiles.getY() < GAME_HEIGHT) )
                    mapTiles.draw();                // draw tile
            
        }
    }

}



void Map::readMap()
{
	std::ifstream mapFile("rsc\\maptile.txt");

	if ( mapFile.is_open() )
	{
		for ( int x=0; x < MAP_HEIGHT; x++)
		{
			for ( int y=0; y < MAP_WIDTH; y++)
			{
				mapFile >> mapArray[ x ] [ y ];
			}
		} 

		mapFile.close();
	} else std::cout << "Unable to open file";
};




I got this problem that if I move the "viewport" around in a special angle I get these blue stripes around my tiles. No idea why.

If I have to elaborate just tell me what part has to be elaborated on and it will be done!

And yeah Butch. The player shall drown! Muahahahahahahahha *cough*
Was This Post Helpful? 0
  • +
  • -

#29 Silver1992  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 13-August 12

Re: Game Logic

Posted 07 January 2013 - 04:36 AM

Ok, this was posted somewhere during my I'm asleep and I want to sleep phase. Forgot add a bunch of stuff.

First off. The file from which the readMap() function gets the data from looks like this.
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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1 1 2 4 4 4 4 3 4 4 4 4 2 1 1 2 2 3 1 4 2 2 1 
1 2 3 4 1 3 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 3 1 4 2 2 1 
1 2 3 4 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 4 3 2 1 
1 2 3 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 3 2 1 
1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 
1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 
1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


Posted Image
Is what my tileset looks like at the moment.

Here is how it looks if I move the game up+left. Get these weird blue streaks everywhere.
Here is how the game looks when I first start it. Please note that it's possible for me to regain the "normal" look by simply moving the window around for awhile and at some point it goes back too looking smooth.


I hope to smooth-en out things with the graphics before adding my player and working on the collision detection and tile-types.
Was This Post Helpful? 0
  • +
  • -

#30 Silver1992  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 13-August 12

Re: Game Logic

Posted 08 January 2013 - 05:46 PM

Cant for the life of me figure out why I'm getting the blue streaks.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3