Help optimizing zombie-shooter code, and 'being eaten alive' code

Started by Egmundo Huevoz, Mon 25/12/2017 20:57:22

Previous topic - Next topic

Egmundo Huevoz

Hello! It's me again, lol. I wonder how many posts I can make before I start irritating people (laugh)
I'll cut to the chase: I'm programming a room in which 3 zombies (objects) walk towards the bottom of the screen. When they reach that, if you haven't killed them, you lose. My question is HOW to know when the object has reached that part of the room. I've tried regions and hotspots, to no avail.

The other thing is that right now I have 3 zombies, but I might add some more later on. And I'm sure the code can be optimized so I don't have to write so much. This is the code:
Code: ags
// room script file


function ZShoot(this Object*) {
//if you have bullets, and you hit your target:
if (btnAmmo.Width!=0){
  btnAmmo.Width-=20;
  Score++;
  String Scoretxt = String.Format("Score: %d", Score);
  lblScoreAmmo.Text=Scoretxt;
  aGunshot.Play();
  aZombie.PlayQueued();
  ZHP[this.ID]--; //zombie hit points
  }
//if your magazine is empty
else{
  aEmptygun.Play();}
}

function on_mouse_click(MouseButton button){
if (IsGamePaused() == 1) {
  // Game is paused, so do nothing (ie. don't allow mouse click)
  }
else if (button == eMouseLeft){
  if (mouse.Mode == eModeAttack) { //custom mode I made to shoot things
    Object* o = Object.GetAtScreenXY(mouse.x, mouse.y);
    if (o != null) o.ZShoot();
  }
}
}

function room_RepExec()
{
  
//zombie death
if (ZHP[0]==0)
  oZ0.Visible=false;
if (ZHP[1]==0)
  oZ1.Visible=false;
if (ZHP[2]==0)
  oZ2.Visible=false;
  
//zombie movement
if (!oZ0.Moving){
  oZ0.Move(Random(641), oZ0.Y+100, Velocidad);
}
if (!oZ1.Moving){
  oZ1.Move(Random(641), oZ1.Y+100, Velocidad);
}
if (!oZ2.Moving){
  oZ2.Move(Random(641), oZ2.Y+100, Velocidad);
}

//highlight reticule over target

if (GetLocationType(mouse.x,mouse.y) == eLocationObject){
  mouse.ChangeModeGraphic(eModeAttack, 202);}

else{
  mouse.ChangeModeGraphic(eModeAttack, 197);
}

}


function room_Load()
{
//definition of variables

Score=0;
Reloads=5; //how many maganizes you have
//Objetivo=5; //ignore this
Velocidad=1; //zombie movement speed

//some other useful stuff
player.Clickable=0;
player.Transparency=100;
mouse.Mode=eModeAttack;
mouse.ChangeModeGraphic(eModeAttack, 197);
mouse.ChangeModeHotspot(eModeAttack, 20, 21);
gAmmo.Visible=true;
gScoreAmmo.Visible=true;
String Objetivotxt = String.Format("Objetivo: %d", Objetivo);
lblObjetivoammo.Text=Objetivotxt;
String Reloadtxt = String.Format("Cartuchos restantes: %d", Reloads);
lblAmmoReloads.Text=Reloadtxt;
  
  
//zombies hp, view and animation.
ZHP[0]=1;
ZHP[1]=1;
ZHP[2]=1;
oZ0.SetView(89);
oZ1.SetView(89);
oZ2.SetView(89);
oZ0.Animate(0, 3, eRepeat, eNoBlock);
oZ1.Animate(0, 3, eRepeat, eNoBlock);
oZ2.Animate(0, 3, eRepeat, eNoBlock);

}


Khris

Quote from: Egmundo Huevoz on Mon 25/12/2017 20:57:22My question is HOW to know when the object has reached that part of the room. I've tried regions and hotspots, to no avail.
My guess is objects don't trigger "walks onto" events.

Anyway, why not simply check the Y coordinate? All you need is
Code: ags
  // in rep_exe
  if (oZombie1.Y > 390) ...



Also, completely unrelated: I've seen this: Random(641) multiple times now. Not only is the parameter included in the range, but the last coordinate of a 640 pixels wide screen is 639. So you would need Random(639) to get a random pixel. Again though, Objects are positioned using the bottom left corner, not the bottom center like characters.

Egmundo Huevoz

Yeah, you're right! I did do that after a while. But how do I trigger that with any object reaching the end of the room? Say, if I had 20 or 30 zombies...

And as for the 641, that was an error of my part (laugh) Luckily it wasn't game breaking.

EDIT: if it's easier, I can replace the objects with characters. As long as I can make a more general function so when any zombie reaches the bottom of the screen, I die.

Matti

Objects have numbers, so something like this should work:

Code: ags

int zombie = 0;

while (zombie < 30) // or whatever the amount of zombie objects is
{
  if (object[zombie].Y > 390)...
  zombie ++;
}


EDIT: Of course, the zombie objects must be consecutive and no other objects should be between 0 and 29 in this example. If there were others, you'd have to make exceptions in the if clause.

Cassiebsg

Keep in mind that there's a 30 object limit per room... (roll)
There are those who believe that life here began out there...

Matti

Ah yes, I wasn't sure if the object limit still exists. But you can always re-use objects if they aren't all supposed to be on screen at the same time.

Crimson Wizard

Quote from: Cassiebsg on Tue 26/12/2017 00:07:28
Keep in mind that there's a 30 object limit per room... (roll)

40!

Also, for enemies I'd really use characters. They are more flexible, unlimited, and could be used in multiple rooms.
For example, consider dropped items: all characters have inventories.

Egmundo Huevoz

Yeah CW, I used objects because I can see exactly where they're gonna begin in the room, and quickly move them. But I'm gonna change them to characters right now. Do you know any way to trigger some code when ANY character reaches the bottom of the screen?

Matti

Just replace object with character in the code I posted.

SMF spam blocked by CleanTalk