Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
0 registered members (), 17,416 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
npc movement - 2d game #291446
09/25/09 11:03
09/25/09 11:03
Joined: Aug 2009
Posts: 26
Goiânia - Brazil
D
dmBlack Offline OP
Newbie
dmBlack  Offline OP
Newbie
D

Joined: Aug 2009
Posts: 26
Goiânia - Brazil
Hi there!

I am losing my hair here with this stuff. I am trying to make the npc panel to move based on the current player panel position. It is a simple 2d game, with no entity, just panels. When the the panels colide, a new perspective is shown for the combat or some other interaction (like in that old conan game).

The npc either stays still or move to southwest doesn´t matter where is the player... here is part of the code relative to the problem:


function npcMove(var d)
{
var dir = d;
if (dir == 0)
{
npcX += 1;
npcY += 1;
}
if (dir == 1)
{
npcX += 1;
npcY -= 1;
}
if (dir == 2)
{
npcX -= 1;
npcY += 1;
}
if (dir == 3)
{
npcX -= 1;
npcY -= 1;
}
}


function aiScript()
{
//I´ll simplify the panels (I should be working right now)
// the X´s and Y´s are actually center_x and center_y of the panels
if (npcX < pX && npcY < pY) //go southeast
npcMove(0);
else if (npcX < pX && npcY > pY) //go northeast
npcMove(1);
else if (npcX > pX && npcY < pY) //go southwest
npcMove(2);
else if (npcX > pX && npcY > pY) //go northwest
npcMove(3);
}

function main()
{
//some initializations
while(1)
{
gameLoop();
//movement input for player panel and other stuff
aiScript();
wait(5);
}
}

So I need some help with this issue... thanks!

Re: npc movement - 2d game [Re: dmBlack] #291448
09/25/09 11:19
09/25/09 11:19
Joined: Jan 2007
Posts: 2,247
Deutsch Niedersachsen
Puppeteer Offline
Expert
Puppeteer  Offline
Expert

Joined: Jan 2007
Posts: 2,247
Deutsch Niedersachsen
try:


function aiScript()
{
//I´ll simplify the panels (I should be working right now)
// the X´s and Y´s are actually center_x and center_y of the panels
while(1)
{
if (npcX < pX && npcY <= pY) //go southeast
npcMove(0);
else if (npcX < pX && npcY >= pY) //go northeast
npcMove(1);
else if (npcX > pX && npcY <= pY) //go southwest
npcMove(2);
else if (npcX > pX && npcY >= pY) //go northwest
npcMove(3);
wait(1);
}
}


Formally known as Omega
Avatar randomness by Quadraxas & Blade
http://omegapuppeteer.mybrute.com
Re: npc movement - 2d game [Re: Puppeteer] #291821
09/28/09 10:57
09/28/09 10:57
Joined: Aug 2009
Posts: 26
Goiânia - Brazil
D
dmBlack Offline OP
Newbie
dmBlack  Offline OP
Newbie
D

Joined: Aug 2009
Posts: 26
Goiânia - Brazil
Hmm that didn´t work... is it not possible to change panel values during runtime? Because it works fine with the character panel. Something I´ve noticed during the tests is that no matter what I do, the npc panel always move following the first "if" case, even if it´s position does not meet the reqs....

Re: npc movement - 2d game [Re: dmBlack] #291874
09/28/09 17:35
09/28/09 17:35
Joined: Oct 2004
Posts: 1,655
T
testDummy Offline
Serious User
testDummy  Offline
Serious User
T

Joined: Oct 2004
Posts: 1,655
Code:
// ???
// 	move difference between player and npc positions
diffX = pX - npcX;
diffY = pY - npcY;
//	move difference to direction
dirX = sign(diffX);
dirY = sign(diffY);
// 	add direction as distance each frame
npcX += dirX;
npcY += dirY;
// ???



Re: npc movement - 2d game [Re: testDummy] #292099
09/30/09 10:56
09/30/09 10:56
Joined: Aug 2009
Posts: 26
Goiânia - Brazil
D
dmBlack Offline OP
Newbie
dmBlack  Offline OP
Newbie
D

Joined: Aug 2009
Posts: 26
Goiânia - Brazil
Do you guys know any other kind of aproach for non character movement? I am trying to make npc panels to move depending on the character´s panel position. It is a function that runs in the main loop (like above) but it isn´t working. I´ve double checked the logic arguments and they are correct. Is there another way, I mean what kind of AI you guys have used with success in your projects... I don´t actually need the code just a basic idea...

Re: npc movement - 2d game [Re: dmBlack] #292180
09/30/09 21:03
09/30/09 21:03
Joined: Oct 2004
Posts: 1,655
T
testDummy Offline
Serious User
testDummy  Offline
Serious User
T

Joined: Oct 2004
Posts: 1,655
a sample (probably irrelevant):
Code:
/* 
	2D TEXT follow sample
	lang: Lite-C 1.70?
	move "PL" TEXT with arrows
	"AI" TEXT follows
	2009.09.30a
*/
TEXT* ai_t = {
	pos_x = 10;
	pos_y = 10;
	string("AI");
	layer = 100;
	flags = VISIBLE;
}
TEXT* pl_t = {
	pos_x = 50;
	pos_y = 70;
	string("PL");
	layer = 100;
	flags = VISIBLE;
}
/******************************
	textf_follow
		call once from main
*******************************/
function textf_follow() {
	var dist; dist = 0;
	while(1) {
		pl_t.pos_x += (key_cur - key_cul) *  2.5 * time_step;
		pl_t.pos_y += (key_cud - key_cuu) * 2.5 * time_step;
		dist = sqrt(pow(pl_t.pos_x - ai_t.pos_x, 2) + pow(pl_t.pos_y - ai_t.pos_y, 2));
		if (dist > 5) {
			ai_t.pos_x += sign(pl_t.pos_x - ai_t.pos_x) * 2.5 * time_step;
			ai_t.pos_y += sign(pl_t.pos_y - ai_t.pos_y) * 2.5 * time_step;
		}
		wait(1);
	}
}




Re: npc movement - 2d game [Re: testDummy] #296869
11/03/09 16:10
11/03/09 16:10
Joined: Aug 2009
Posts: 26
Goiânia - Brazil
D
dmBlack Offline OP
Newbie
dmBlack  Offline OP
Newbie
D

Joined: Aug 2009
Posts: 26
Goiânia - Brazil
Thanks guys!
I´ve tryed to keep to player position still and to move the tiles instead... it works! Now the enemy stay still if player is a little far away, and starts chasing the player if he gets too close. Also, when the enemy gets to a certain distance from the player panel, it starts attacking. It´s cool, everything fits now! Thanks!

Re: npc movement - 2d game [Re: dmBlack] #297145
11/05/09 16:49
11/05/09 16:49
Joined: Aug 2009
Posts: 26
Goiânia - Brazil
D
dmBlack Offline OP
Newbie
dmBlack  Offline OP
Newbie
D

Joined: Aug 2009
Posts: 26
Goiânia - Brazil
With the previous problem being solved, there is another issue right now. Each enemy is going to be represented by a panel. How can I optmize the panels that need to be displayed on screen. What I mean is that if the enemy has coordinates outside the screen bounds, his panel shouldn´t waste system resources...

This project is all panel based, all 2d. If I choose to create all panels at game start, let´s say 200 panels, is that going to be an issue?

If I choose to do pan_create, getting panel properties from a struct in example, will it do any good to the game performance? Anyway I don´t know if this method would work, because the enemy panel chases the player panel and then it could get to a coordinate near another panel "creating" area.

Thanks.

Re: npc movement - 2d game [Re: dmBlack] #298447
11/15/09 04:09
11/15/09 04:09
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
FoxHound Offline
Expert
FoxHound  Offline
Expert

Joined: Jun 2004
Posts: 2,234
Wisconsin USA
function npcMove(var d)
{
var dir = d;

That part is redundant. Why make a new var to get the d var info?

also, since only one of those "ifs" could work you should "return" the function to help speed up processing time. I know it's very little time but every bit helps.

Are you making a side scroller game? if so you will need to wait until the player gets within the screen width to start them using any of their main functions, however you will still need to check to see how close they are. If it is not then regular code will need to be used as rendering will not be done on anything outside of the screen view.

I would have it create and destroy the panels as need, as per level. However I would think 200 would be fine.


---------------------
There is no signature here.


QUIT LOOKING FOR ONE!

Moderated by  HeelX, Spirit 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1