Kain5056

Member
  • Content count

    39
  • Joined

  • Last visited

Community Reputation

502 Good

About Kain5056

  • Rank
    Member
  1. Quick question about handling 2d levels

    There should be a single state for gameplay, another for overworld, etc. Data is what changes from stage-to-stage.  A “level manager” is ambiguous and likely not what you want.  The game engine should simply be able to take any stage data and have your character be able to walk through it, etc.     L. Spiro   Thanks for your answer. :-) So, as I see it, I'll have a level (gameplay) state and an overworld state, and have the overworld determine what level file should be loaded, kind of like a glorified menu. Seems simple enough. :-) Thanks again. :-)
  2. 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. :-)
  3. Help understanding Component-Entity systems.

    One more question, about your first example: struct Entity { int Id; std::vector<TComponent*> Components; }; Doesn't that vector cause problems with inheritance? If I try to run a function from a component that inherits from the base component class/struct, won't it only run the base component's function instead of the inheriting component's?
  4. Help understanding Component-Entity systems.

    Thank you very much for your answer. It's a good starting point for me to start learning the more complicated aspects of programming. In the next days I will look at your journal posts and experiment. :-)   One question, though: I want to get an explanation on what exactly is going on in these parts: TComponent(TEntityId entityOwner) : EntityOwner(entityOwner) {} TPosition(float x, float y, TEntityId entityOwner) : TComponent(entityOwner), X(x), Y(y) {} What exactly should I google? The reference on structures does not cover this part. :-/   Based on your second example, I made this little test which I will experiment with in the next days: typedef int entity; struct component { component( entity id ) : ID( id ) {} entity ID; }; struct message : component { std::string Word; message( std::string word , entity id ) : component( id ) , Word( word ) { std::cout << Word << std::endl; } }; int main( int argc , char * argv[] ) { entity e = 123; entity e2 = 456; message( "Hello! :-D I'm entity 1!" , e ); message( "...And I'm entity 2! Goodbye! :-)" , e2 ); std::cin.ignore(); return 0; } I know there won't be actual components like "message", it's just my way of testing the implementation with a "Hello world" type of test program. :-)
  5. 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. :-)
  6. Simple parsing.

    Thank you very much SmkViper, this is exactly what I wanted. :-) As Krypt0n also suggested, I will eventually look into existing parsers, but for the time being I'd like to play a bit with this subject myself, at least for the small test programs.   Thanks again. :-)
  7. Simple parsing.

    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. :-)
  8. OK, I googled it (didn't know exactly what to google at the time) and found the solution. I downgraded the API Level to 17 just to be sure (Android version 4.2.2) and it works like a charm, both in the emulator and on my physical phone. :-) Thanks for the help. :-)   A question though: Do I even need the API Level 20 stuff? Or is it safe to just uninstall them and just work with Level 17? Because the whole thing takes a billion trillion (rough estimate) Gigabytes on my HDD and it's a bit annoying since it's almost full... :-/
  9. Okay, here is what the LogCat gives me: 07-30 20:56:19.420: E/AndroidRuntime(871): FATAL EXCEPTION: main 07-30 20:56:19.420: E/AndroidRuntime(871): Process: com.example.testgame, PID: 871 07-30 20:56:19.420: E/AndroidRuntime(871): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testgame/com.example.testgame.MainActivity}: android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar. 07-30 20:56:19.420: E/AndroidRuntime(871): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197) 07-30 20:56:19.420: E/AndroidRuntime(871): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258) 07-30 20:56:19.420: E/AndroidRuntime(871): at android.app.ActivityThread.access$800(ActivityThread.java:138) 07-30 20:56:19.420: E/AndroidRuntime(871): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209) 07-30 20:56:19.420: E/AndroidRuntime(871): at android.os.Handler.dispatchMessage(Handler.java:102) 07-30 20:56:19.420: E/AndroidRuntime(871): at android.os.Looper.loop(Looper.java:136) 07-30 20:56:19.420: E/AndroidRuntime(871): at android.app.ActivityThread.main(ActivityThread.java:5026) 07-30 20:56:19.420: E/AndroidRuntime(871): at java.lang.reflect.Method.invokeNative(Native Method) 07-30 20:56:19.420: E/AndroidRuntime(871): at java.lang.reflect.Method.invoke(Method.java:515) 07-30 20:56:19.420: E/AndroidRuntime(871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 07-30 20:56:19.420: E/AndroidRuntime(871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 07-30 20:56:19.420: E/AndroidRuntime(871): at dalvik.system.NativeStart.main(Native Method) 07-30 20:56:19.420: E/AndroidRuntime(871): Caused by: android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar. 07-30 20:56:19.420: E/AndroidRuntime(871): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:275) 07-30 20:56:19.420: E/AndroidRuntime(871): at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2872) 07-30 20:56:19.420: E/AndroidRuntime(871): at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3129) 07-30 20:56:19.420: E/AndroidRuntime(871): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:303) 07-30 20:56:19.420: E/AndroidRuntime(871): at android.app.Activity.setContentView(Activity.java:1930) 07-30 20:56:19.420: E/AndroidRuntime(871): at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:217) 07-30 20:56:19.420: E/AndroidRuntime(871): at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110) 07-30 20:56:19.420: E/AndroidRuntime(871): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:77) 07-30 20:56:19.420: E/AndroidRuntime(871): at com.example.testgame.MainActivity.onCreate(MainActivity.java:15) 07-30 20:56:19.420: E/AndroidRuntime(871): at android.app.Activity.performCreate(Activity.java:5242) 07-30 20:56:19.420: E/AndroidRuntime(871): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 07-30 20:56:19.420: E/AndroidRuntime(871): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161) 07-30 20:56:19.420: E/AndroidRuntime(871): ... 11 more
  10. 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. :-)
  11. Homing missile problem

    Welp, no wonder my code does not work. You are right, it seems that SFML and ActionScript handle rotation a bit differently. I think that in ActionScript the missile points up at 90 degrees, but in SFML it points down. All I need now to adjust the code it to find out how exactly does ActionScript handle rotation. Direction, degrees, and all that. Then, I'll be able to adjust the code accordingly. I'll try searching online tomorrow.   Thank you for explaining all this to me. :-)
  12. Homing missile problem

    After a bit of thought and some calculations, I discovered that the conversion of range from [0,360] to [180,-180] seems to be as simple as this: float current = 180 - rocket_sprite.getRotation(); With this, the code should work just as in the ActionScript example, since now it's all in the same range and at the same units. It does not. The missile goes shakey, and there's no easing. I can't understand why.   I'll keep looking into it, but right now I'm baffled. Why does it work there, but not here?   For reference, HERE's the ActionScript tutorial. The example of how it should work is under "Step 10: Less Precision for Better Looks".   ...And here is my current code: #include <math.h> #define PI 3.14159265 class rocket { private: sf::Texture rocket_texture; sf::Sprite rocket_sprite; sf::Vector2f position; float 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 ); float targetx = mouse_position.x - position.x; float targety = mouse_position.y - position.y; rotation = atan2( targety , targetx ) * ( 180 / PI ); int ease = 5; float current = 180 - 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; }
  13. Homing missile problem

    Not to sound ungrateful here, in fact I'm going to study intensely all the code you all provided, and I am extremely grateful for all the information, but for the moment I'd like to stick to the tutorial code as much as possible so I can keep following it. I can't help but think that there must be a simpler way to do it.   In fact, I have noticed that in the line rotation = atan2( targety , targetx ) * ( 180 / PI ); the " * ( 180 / PI ) " part  converts radians to degrees, meaning that all the calculations are done in degrees. float current = rocket_sprite.getRotation(); also returns degrees, as I understand, so the only problem is the range.     I have tried calculating the current angle manually, using this code: float current = atan2( rocket_sprite.getPosition().y , rocket_sprite.getPosition().x ) * ( 180 / PI ); As you might have guessed, it didn't make any difference. But I still think that there must be a solution along those lines with minimal changes to the tutorial code. I'm just not good enough at math to find it.   Still, I *do* appreciate immensely all the examples and information you provided, and I *will* study them. Just after I manage to finish the tutorial. :-)   So, if you are still willing to help me convert just the range of "getRotation()", I'm still open to suggestions. :-)
  14. Homing missile problem

    @Álvaro: Thank you so much for the code example. It's complexity is a bit out of my league for the the time being, though. ^^'  I will study it, but it's a bit more complex than I would like. haegarr's code is more like what I'm looking for, but, as mentioned, it does not really work. Still, thank you both so much. :-)   To tell you the truth, I don't really know what parts of the code are in radiants and what are in degrees. I assumed it was all in degrees up until now.   I also know pretty much nothing about ActionScript, so the ranges are also a bit of an issue here.   Without the easing, the code works perfectly! The whole problem seems to be just with getRotation() when compared to rotation.   EDIT: Playing with haegarr's code I tried this and it *almost* works, but still not quite there. void rocket::rocket_rotate( sf::RenderWindow * window ) { sf::Vector2i mouse_position = sf::Mouse::getPosition( *window ); float targetx = mouse_position.x - position.x; float targety = mouse_position.y - position.y; rotation = atan2( targety , targetx ) * 180 / PI; int ease = 5; float current = rocket_sprite.getRotation(); current = current <= 180 ? current : current - 360; rotation = rotation <= 180 ? rotation : rotation - 360; 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; rotation += 360 / PI; rocket_sprite.setRotation( rotation ); }
  15. Homing missile problem

    @Orymus3: Thanks, but I'm not sure how to implement your code, or how it will solve my problem. I will look into it more later, though. :-) Thanks.   @haegarr: Thanks for the info, I also think that the problem lies there, but I'm not sure how to fix it. I tried int current = rocket_sprite.getRotation() - 180; but it does not quite work, even though it does make things a bit better. :-)   I'm still open for ideas...