Jump to content

  • Log In with Google      Sign In   
  • Create Account

Kain5056

Member Since 22 Dec 2012
Offline Last Active May 17 2015 05:16 AM

Topics I've Started

Quick question about handling 2d levels

07 May 2015 - 05:01 AM

After a long time doing other things I decided to pick up and finish my first game. It's a 2D Platformer with tile-based levels. However, due to not having coded at all in a long time, I'm a bit rusty and need some advice on how to handle levels.

 

The last thing I did was create a state engine. I want my game to be level based with an overworld where new levels are gradually unlocked, like Super Mario World for example.

 

My question is: Should I create a state for every level, or a single level state that reads from a level manager? Is any of these options better than the other, more common, or outright bad practice? Or it does not really matter as long as it works for me?

 

Thank you very much in advance. :-)


Help understanding Component-Entity systems.

21 February 2015 - 05:57 AM

I'm trying to understand how Component-Entity systems work, but the more I read, the more confused I get. Every article I read does things very differently, and, most of the time, is too complicated for my level of experience to follow. So I'd like some pointers from someone more experienced. Maybe I'm not reading the right articles or tutorials for my case, so you may need to point me to a good source.

 

I'm very inexperienced, so try to not cringe if I write something too insane. '^^

 

This is what confuses me the most right now: I see many articles saying that an entity is nothing more that an integer id. That integer id determines what components the entity will have, with each component having its own id.

 

As I understand it, an entity is, for an oversimplified example, the id number 125, which means it contains the components with the id numbers 1, 2 and 5.

 

It's obvious that I understanding completely wrong. For example, what determines the stats of the entity? What determines the external files (spritesheets etc) that the entity uses? What determines if the entity is alive or dead? What determines the entity's stats and position? Can I get all that from just a number?

 

So, here's an example: I have a simple set of components, named "message" and "name", inheriting from a base component class. Each component reads a string from an external file. And I also have an entity struct that only has an int named "id". Can someone please explain how do I connect the components to the entity? Here's some oversimplified, probably atrocious code:

struct entity { int id; };

class component
{
public:
    int id = 0;
    void load( std::string file );
    void run();
};

void component::load( std::string file ) {}
void component::run() {}

class message : public component
{
public:
    int id = 1;
    std::string words;
    void show_message();
    void load( std::string file );
    void run();
};

void message::show_message() { std::cout << words << std::endl; }

void message::load( std::string file )
{
    std::ifstream infile( file.c_str() );
    std::getline( infile , words );
}

void message::run() { show_message(); }

class name : public component
{
public:
    int id = 2;
    std::string Name;
    void show_name();
    void load( std::string file );
    void run();
};

void name::show_name() { std::cout << Name << ": "; }

void name::load( std::string file )
{
    std::ifstream infile( file.c_str() );
    std::getline( infile , Name );
}

void name::run() { show_name(); }

Thank you in advance. :-)


Simple parsing.

20 February 2015 - 08:09 AM

Sorry for the kind of useless topic, but I'd like someone more experienced to tell me if I go the right way about it.

 

I'm trying to parse very simple commands from an external text file using a vector of char[]'s and std::ifstream infile, but it seems a little clunky to me, so I have doubts about it.

 

In this test program, I'm trying to get the function to recognize and react to new lines and a smiley face.

 

This is the text file:

 

data.txt

Hello, I am a text file! SMILEY And this is a test!
SMILEY

SMILEY A very happy test! SMILEY

...And this is the program I test the method with:

 

main.cpp

#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>


struct character { char ch[10]; };

int main(int argc , char * argv[] )
{
    std::vector<character> word_list;
    character ch_main;

    std::ifstream infile( "data.txt" );
    while( infile >> ch_main.ch )
    {
        word_list.push_back( ch_main );
        if( infile.peek() == '\n' )
        {
            character ch_temp;
            strcpy( ch_temp.ch , "LINE" );
            word_list.push_back( ch_temp );
        }
    }

    for( int i = 0 ; i < word_list.size() ; i++ )
    {
        if( !strcmp( word_list[i].ch , "LINE" ) ) std::cout << std::endl;
        else if( !strcmp( word_list[i].ch , "SMILEY" ) ) std::cout << ":-) ";
        else
        {
            std::cout << word_list[i].ch;
            std::cout << " ";
        }
    }


    std::cin.ignore();
    return 0;
}

My intention is to find a simple method to read external files of component lists for a component based entity system. I have in mind something like this:

 

enemy behavior.txt

walk : looking_direction , speed 10 , seconds 2
shoot : bullet.txt , looking_direction , times 3
turn_around

Bear in mind I have zero experience with component systems, so I might go completely wrong about the whole thing.

 

Thank you in advance. :-)


Hello World App won't run in emulator

30 July 2014 - 02:29 PM

Hi, I'm trying to learn how to make my first "Hello World" application for Android, in Java with Eclipse. But when I try to run it in the Emulator, all I get is the "Unfortunately, Hello World has stopped" message.

 

Here is my code:

package com.example.testgame;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = new TextView(this);
        tv.setText("Yo Android World!");
        setContentView(tv);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

All I did was adding "import android.widget.TextView;", "TextView tv = new TextView(this);", "tv.setText("Yo Android World!");" and "setContentView(tv);" to the default New Project code, following THIS Tutorial.

 

Also, if you could point me to a more up-to-date Tutorial series for beginners, it would really help. :-)

 

Thank you in advance. :-)


Homing missile problem

05 June 2014 - 07:14 AM

I'm trying to follow this Actionscript tutorial for homing missiles, translating it to C++ with SFML.

 

But I seem to have a problem with this piece of code:

 

In Actionscript:

var rotation:int = Math.atan2(targetY, targetX) * 180 / Math.PI;
        if (Math.abs(rotation - missile.rotation) > 180)
        {
            if (rotation > 0 && missile.rotation < 0)
                missile.rotation -= (360 - rotation + missile.rotation) / ease;
            else if (missile.rotation > 0 && rotation < 0)
                missile.rotation += (360 - rotation + missile.rotation) / ease;
        }
        else if (rotation < missile.rotation)
            missile.rotation -= Math.abs(missile.rotation - rotation) / ease;
        else
            missile.rotation += Math.abs(rotation - missile.rotation) / ease;

Translated in C++ with SFML:

int current = rocket_sprite.getRotation();
    if( abs( rotation - current ) > 180 )
    {
        if( rotation > 0 && current < 0 ) rotation -= ( 360 - rotation + current ) / ease;
        else if( current > 0 && rotation < 0 ) rotation += ( 360 - rotation + current ) / ease;
    }
    else if( rotation < current ) rotation -= abs( current - rotation ) / ease;
    else rotation += abs( rotation - current ) / ease;

With this code, the missile acts crazy, getting stuck in places, continually rotating in place for no reason and other stuff like that.

 

I suspect that the problem lies in the fact that .getRotation() returns absolute values, but I don't know how to even test it.

 

Here is my complete code:

#include <math.h>
#define PI 3.14159265

class rocket
{
private:
    sf::Texture rocket_texture;
    sf::Sprite rocket_sprite;
    sf::Vector2f position;
    int rotation;

public:
    rocket();
    bool rocket_load();
    void rocket_show( sf::RenderWindow * window );
    void set_position( sf::Vector2f new_position );
    void rocket_rotate( sf::RenderWindow * window );
    void rocket_move();
};

rocket::rocket()
{
    rotation = 0.f;
}

bool rocket::rocket_load()
{
    rocket_texture.loadFromFile( "rocket.png" );
    rocket_sprite.setTexture( rocket_texture );

    return true;
}

void rocket::rocket_show( sf::RenderWindow * window )
{
    rocket_sprite.setOrigin( rocket_texture.getSize().x / 2 , rocket_texture.getSize().y / 2 );
    rocket_sprite.setPosition( position );
    window->draw( rocket_sprite );
}

void rocket::set_position( sf::Vector2f new_position )
{
    position = new_position;
}

void rocket::rocket_rotate( sf::RenderWindow * window )
{
    sf::Vector2i mouse_position = sf::Mouse::getPosition( *window );
    int targetx = mouse_position.x - position.x;
    int targety = mouse_position.y - position.y;

    rotation = atan2( targety , targetx ) * 180 / PI;

    int ease = 5;
    int current = rocket_sprite.getRotation();
    if( abs( rotation - current ) > 180 )
    {
        if( rotation > 0 && current < 0 ) rotation -= ( 360 - rotation + current ) / ease;
        else if( current > 0 && rotation < 0 ) rotation += ( 360 - rotation + current ) / ease;
    }
    else if( rotation < current ) rotation -= abs( current - rotation ) / ease;
    else rotation += abs( rotation - current ) / ease;

    rocket_sprite.setRotation( rotation );
}

void rocket::rocket_move()
{
    int speed = 5;
    float speedx = 0;
    float speedy = 0;
    speedx = speed * ( 90 - abs( rotation ) ) / 90;
    if( rotation < 0 ) speedy = -speed + abs( speedx );
    else speedy = speed - abs( speedx );
    position.x += speedx;
    position.y += speedy;
}

If anyone can point me to the right direction, I would really appreciate it. :-)

Thank you in advance. :-)


PARTNERS