Moving a char w/o ifs, keyup, keydown and global codes..

Non-platform specific questions.

Moving a char w/o ifs, keyup, keydown and global codes..

Postby poopbrigade » Mon Feb 07, 2011 12:30 pm

heres a simple code to make your char move without any ifs. XD

in Draw actor -> Script editor
Code: Select all
char *key=GetKeyState();



y-=(5*key[KEY_w])*((1/(key[KEY_d]+key[KEY_a]+key[KEY_s]+1)));
y+=(5*key[KEY_s])*((1/(key[KEY_d]+key[KEY_a]+key[KEY_w]+1)));
x-=(5*key[KEY_a])*((1/(key[KEY_d]+key[KEY_w]+key[KEY_s]+1)));
x+=(5*key[KEY_d])*((1/(key[KEY_w]+key[KEY_a]+key[KEY_s]+1)));

no_if_wsad.zip
XD Happy..
(8.54 KiB) Downloaded 73 times

I dunno if someone has already done this, but I'm just post'n might help some1 in someway XD


Inspired by DST's post in
viewtopic.php?f=5&t=6854&start=15


1+ to DST XD
When life gives you oranges, make orange juice. When life gives you poop, make fertilizer not poop juice.

Image
Current project __ CorPsEdEaRtH
Estimated work done __ 12%
User avatar
poopbrigade
 
Posts: 129
Joined: Thu Aug 27, 2009 12:17 pm
Location: In Earth. Being Burried XD
Score: 46 Give a positive score

Re: Moving a char w/o ifs, keyup, keydown and global codes..

Postby Game A Gogo » Mon Feb 07, 2011 8:54 pm

Code: Select all
char*key=GetKetState();
y+=((5*key[KEY_s])*((1/(key[KEY_d]+key[KEY_a]+key[KEY_w]+1))))-((5*key[KEY_w])*((1/(key[KEY_d]+key[KEY_a]+key[KEY_s]+1))));
x+=((5*key[KEY_d])*((1/(key[KEY_w]+key[KEY_a]+key[KEY_s]+1))))-((5*key[KEY_a])*((1/(key[KEY_d]+key[KEY_w]+key[KEY_s]+1))));


you have y+= and y-=. they both do the "same" thing, but reversed, so it's easy to intergrate it into one single function. since if you add a negative, it's like substration :)
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Moving a char w/o ifs, keyup, keydown and global codes..

Postby poopbrigade » Mon Feb 07, 2011 11:34 pm

Thanx learned a new thing XD
do you have anyother tricks in your sleve?
+1
When life gives you oranges, make orange juice. When life gives you poop, make fertilizer not poop juice.

Image
Current project __ CorPsEdEaRtH
Estimated work done __ 12%
User avatar
poopbrigade
 
Posts: 129
Joined: Thu Aug 27, 2009 12:17 pm
Location: In Earth. Being Burried XD
Score: 46 Give a positive score

Re: Moving a char w/o ifs, keyup, keydown and global codes..

Postby Game A Gogo » Mon Feb 07, 2011 11:49 pm

if conditions doesn't limit themselves to "if(condition){}"
here's something interesting when you can jump twice, so you would need to hold numbers of jump in a var.
so to make your actor jump, you could do this:
Code: Select all
yvelocity-=(jump_count>0)*key[KEY_space];

as you can see, no if's is used. But there is a still a condition check in process, so it requires pretty much the same amount of cpu process.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Moving a char w/o ifs, keyup, keydown and global codes..

Postby poopbrigade » Mon Feb 07, 2011 11:59 pm

WoW XD I could give you another point but I've used it already.
When life gives you oranges, make orange juice. When life gives you poop, make fertilizer not poop juice.

Image
Current project __ CorPsEdEaRtH
Estimated work done __ 12%
User avatar
poopbrigade
 
Posts: 129
Joined: Thu Aug 27, 2009 12:17 pm
Location: In Earth. Being Burried XD
Score: 46 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest