Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Funky player logic.

This thread is locked; no one can reply to it. rss feed Print
Funky player logic.
julian_boolean
Member #8,201
January 2007

It's not much, but I was curious if this looks okay. It's basically for your typical hack and slash type game.

1 if(key[KEY_A])
2 player->attack_mode(true);
3 
4 if (player->attack_mode(true))
5 {
6 if (key[KEY_W])
7 player->is_defending();
8
9 if (key[KEY_E])
10 player->is_attacking();
11
12 if (player->is_walking()) // which i guess will draw different frames for when the player is walking/idling in attack mode
13 // do something
14 }
15
16 else
17 if (key[KEY_W])
18 player->says("I can't do that."); // should i be doing this here or inside the is_defending/is_attacking functions?
19
20 if (key[KEY_E])
21 player->says("I can't do that.");
22 
23 if (player->is_walking())
24 // do something

Also, how can I make toggleable buttons? Since I assume that key[KEY_A] (the key to switch attack mode on and off) will need to be toggleable. Right now if you press it, it would probably just turn attack mode on indefinitely.

aadfo824
Member #7,265
May 2006

For toggling, try:

//in loop or descision-making function
if(key[KEY_A])
     player->toggle_attack_mode();

//in toggle attack mode funtion
player->attack_mode = !player->attack_mode;

However, you should probably set some kind of delay, so it only toggles once every, say 500 ms.

I'm not an expert so I won't reccomend where things should go. (I don't know myself)

piccolo
Member #3,163
January 2003
avatar

this is a little better.

1if(key[KEY_A])
2 player->attack_mode(true);
3 
4 if (player->attack_mode(true))
5 {
6 if (key[KEY_W])
7 player->is_defending();
8
9 if (key[KEY_E])
10 player->is_attacking();
11
12 if (player->is_walking()) // which i guess will draw different frames for when the player is walking/idling in attack mode
13 // do something
14player->attack_mode(false);
15 }
16
17 else
18 if (key[KEY_W])
19 player->says("I can't do that."); // should i be doing this here or inside the is_defending/is_attacking functions?
20
21 if (key[KEY_E])
22 player->says("I can't do that.");
23 
24 if (player->is_walking())

wow
-------------------------------
i am who you are not am i

julian_boolean
Member #8,201
January 2007

aadfo824:

cPlayer* player;

How would the cPlayer class know what the instance "player" is when it's being declared in a different class?

this.toggle_attack_mode() = !this.toggle_attack_mode(); ??? Yes, no?

piccolo:

By putting player->attack_mode(false) inside "if (player->attack_mode(true))", wouldn't that automatically turn it to false as soon as it becomes true?

piccolo
Member #3,163
January 2003
avatar

so your saying that these are just animations and not logic functions?

if (key[KEY_W])
player->is_defending();

if (key[KEY_E])
player->is_attacking();

if (player->is_walking()) // which i guess will draw different frames for when the player is walking/idling in attack mode
// do somethin

wow
-------------------------------
i am who you are not am i

julian_boolean
Member #8,201
January 2007

To tell you the truth I have no idea what I'm doing. Here's some different stuff.

1void cPlayer::draw(BITMAP *buffer)
2{
3 switch (pose)
4 {
5 case IDLE:
6 {
7 // do the idle animation
8 // use "direction" to determine which way the player is facing
9 }break;
10 
11 case WALK:
12 {
13 // do the walk animation
14 // use "direction" to determine which way the player is facing
15 }break;
16 
17 case ATTACK:
18 {
19 // do the attack animation based on what weapon the player is wielding
20 // use "direction" to determine which way the player is facing
21 }break;
22
23 case DEFEND:
24 {
25 // do the defend animation
26 // use "direction" to determine which way the player is facing
27 }break;
28 }
29}
30 
31void cPlayer::is_attacking()
32{
33 pose == ATTACK;
34 
35 if (enemy_is_in_scope)
36 // engage it
37}
38 
39void cPlayer::is_walking()
40{
41 if (key[KEY_UP])
42 {
43 direction == NORTH;
44 
45 if (!collide)
46 {
47 pose == WALK;
48 y -= MOVE_SPEED;
49 }
50
51 else
52 pose == IDLE;
53 }
54 
55 ...
56 
57 ...
58 
59 ...
60 
61 else
62 pose == IDLE;
63}

piccolo
Member #3,163
January 2003
avatar

ok its a Zelda style game right?

so just let those be logic functions let the logic functions toggle a globe variable
named pose. every think looks good. your draw is going to need a little more to it though. i would sagest to get the idle and walk working first.

use the case statement to setup refence to a sprite sheet. then use someingthing to flip the frames each program loop.

i have a MMRPG in the depot take a look at it in going to update in right now. may 07 2007.
edit:http://www.allegro.cc/depot/Thegame/

wow
-------------------------------
i am who you are not am i

julian_boolean
Member #8,201
January 2007

The whole looping through a sprite sheet (assuming that each sheet would contain the base model of a character's every direction and action, like walking left, attacking left, etc) is what I'm not even remotely sure how to do, especially when trying to incorporate direction into it.

piccolo
Member #3,163
January 2003
avatar

ok my game source is updated. before you look at my game to to this site this is the best allegro game site in the world.

http://agdn.netfirms.com/main/
then got to Examples & Misc Downloads

witch will put you here http://danielh7.tripod.com/main/
then down load lessons 1-7 this is where a lot of people on allegro started. it goes step by step though all that good stuff that makes up a game using just allegro no additional libs.

edit: i would love to help keep this site updated.

wow
-------------------------------
i am who you are not am i

julian_boolean
Member #8,201
January 2007

Thanks a bunch. :) This seems to be really helpful for the most part: http://agdn.netfirms.com/main/html/zelda.htm

What the hell is up with this though?

1 if (key[KEY_UP])
2 {
3 player.move(UP);
4 }
5 else
6 {
7 if (key[KEY_DOWN])
8 {
9 player.move(DOWN);
10 }
11 else
12 {
13 if (key[KEY_LEFT])
14 {
15 player.move(LEFT);
16 }
17 else
18 {
19 if (key[KEY_RIGHT])
20 {
21 player.move(RIGHT);
22 }
23 }
24 }
25 }

Why not this?

  if (key[KEY_UP])
    player.move(UP);

  if (key[KEY_DOWN])
    player.move(DOWN);

  if (key[KEY_LEFT])
    player.move(LEFT);

  if (key[KEY_RIGHT])
    player.move(RIGHT);

piccolo
Member #3,163
January 2003
avatar

its easyer to read and understand for new programmers

example num1 = num1 + num2;

num1+=num2;

the first one is easer to understand.

wow
-------------------------------
i am who you are not am i

julian_boolean
Member #8,201
January 2007

Fair enough.

Hows this look?!

1 if (player->is_alive()) // or something
2 {
3 if (key[KEY_UP] && key[KEY_LEFT])
4 player->move(NORTHWEST);
5 
6 if (key[KEY_UP] && key[KEY_RIGHT])
7 player->move(NORTHEAST);
8 
9 if (key[KEY_DOWN] && key[KEY_RIGHT])
10 player->move(SOUTHEAST);
11 
12 if (key[KEY_DOWN] && key[KEY_LEFT])
13 player->move(SOUTHWEST);
14 
15 if (key[KEY_UP])
16 player->move(NORTH);
17 
18 if (key[KEY_RIGHT])
19 player->move(EAST);
20 
21 if (key[KEY_DOWN])
22 player->move(SOUTH);
23 
24 if (key[KEY_LEFT])
25 player->move(WEST);
26
27 if (key[KEY_A])
28 {
29 player->attack_mode(true);
30
31 if (player->attack_mode(true))
32 {
33 if (key[KEY_W])
34 if(S_shld->is_occupied()) // shield slot
35 player->is_defending();
36
37 if (key[KEY_E])
38 player->is_attacking();
39 }
40 }
41 }

In my eyes, it looks like a whole lot of if statements.. I mean this would all be inside the main logic function (for in-game anyway) and there's still an assload of more stuff that needs to go into it, but is it fine?

piccolo
Member #3,163
January 2003
avatar

its ok for a base i guess

wow
-------------------------------
i am who you are not am i

julian_boolean
Member #8,201
January 2007

http://agdn.netfirms.com/main/html/zelda.htm

Could someone explain this:

void Player::draw(BITMAP *bmp)
{
    draw_sprite(buffer,(BITMAP*)data[GUY000+frame].dat,x_pos,y_pos+48);
}

And this:

    if (dir==LEFT)
    {
        x_pos-=4;
        if (frame<4 || frame>5) frame=4;
        if (x_pos<0)
        {
            x_pos=(GRID_X-1)*TILESIZE;
            map.scroll(LEFT);
            screen_x--;
            map.getGrid(room,screen_x,screen_y);
        }

Is he using seperate bitmaps from the datafile, or sheets? I want to use sheets. And how is he going through the frames to make the animation?

Jonny Cook
Member #4,055
November 2003

Quote:

player->attack_mode(true);
if (player->attack_mode(true))
{
// ...

That's a little redundant.

The face of a child can say it all, especially the mouth part of the face.

Go to: