1
\$\begingroup\$

I am working on a small game in Processing 3. Essentialy I have a small open-world game and I wrote up a enemy AI to make the enemies wander aimlessly back and forth around the gameworld, and chase the player if he strays within the monster's feild-of-vision. This all works great; there's nothing wrong with it.

The issue that is plauging me and I haven't been able solve, is the system that controls collision and player health and life. Basicaly I have two persistant bugs, the first bug is that if the player walks within the monster's field-of-vision and the monster chases the player and the player is able to escape from this monster, then if you approch another monster, will start to chase the player, but even if the monster collides with the player, the player will take no damage, regardless if it took damage or not from the first monster.

The second bug, which is most likely linked to the first bug, is that the whole collision/damage appears to not run unless the player 'spawns' in the feild-of-vision of a monster when the game is run. If the player 'spawns' anywhere else in the gameworld that isn't in a feild-of-vision, then the whole collision system breaks.

I have spent a lot of time trying to fix this, but nothing that I try to change seems to fix it. It appears to be a logic issue, I think, where something breaks the collision/damage system after the player escapes from a monster's feild-of-veiw.

I'm not incuding the sprite and world classes, because all of the collision/damage is done in the included monster class. The code, with the sprite and world classes can be found here: https://github.com/NoahJon3s/New-Journey-The-Game

Any help is appreciated and thanks in advance.

The Code:

GameWorld World;
Monster[] monsters=new Monster[2];

Sprite sprite;

int spriteXPos;
int spriteYPos;

int health=8;

int increment;
int second;

void setup()
{
  size(1200,900);
  //size(1200,900,JAVA2D);
  background(0);
  frameRate(30);

  for(int l=0; l<monsters.length; l++)
  {
    int w=int(random(4));
    int x=int(random(width-100));
    int y=int(random(height-200));
    int z=int(random(width-100));

    monsters[l]=new Monster(w,x,y,z);
  }

  for(int l=0; l<monsters.length; l++)
  {
    monsters[l].Load();
  }

  sprite=new Sprite(int(random(width)),int(random(height)));

  World=new GameWorld();
  World.Load();
  sprite.Load();
}


void draw()
{
  background(0);

  World.Draw();

  for(Monster l: monsters)
  {
    l.Update();
    l.Draw();
  }
  sprite.Move();


  increment++;

  if(increment/30==1)
  {
    second++;
    increment=0;
  }

  if(mousePressed==true)
  {
    if(health==0)
    {
      health=8;
      mousePressed=false;
    }
  }

  println(health);
}



class Monster
{ 
  int monsterWVal;
  int monsterXPos;
  int monsterYPos;
  int monsterZPos;

  PImage monster1;
  PImage monster2;
  PImage monster3;
  PImage monster4;

  float monsterRate=.06;

  int veiwSize=250;
  int monsterOffsetX;
  int monsterOffsetY;


  int Width=100;
  int Height=200;

  boolean InSight;
  boolean Collision;

  Monster(int w,int y,int x,int z)
  {
    monsterWVal=w;
    monsterXPos=y;
    monsterYPos=x;
    monsterZPos=z;
  }

  void Load()
  {
    monster1=loadImage("monster1.png");
    monster2=loadImage("monster2.png");
    monster3=loadImage("monster3.png");
    monster4=loadImage("monster4.png");

    InSight=false;
    Collision=false;
  }

  void Update()
  {
    monsterOffsetX=monsterXPos-veiwSize;
    monsterOffsetY=monsterYPos-veiwSize;

    if(spriteXPos+Width>monsterOffsetX && spriteXPos<monsterOffsetX+veiwSize)
    {
      if(spriteYPos+Height>monsterOffsetY && spriteYPos<monsterOffsetY+veiwSize)
      {
        InSight=true;
      }
    }


    if(InSight==true)
    {
       float targetX=spriteXPos;
       float dx=targetX-monsterXPos;
       monsterXPos+=dx*monsterRate;

       float targetY=spriteYPos;
       float dy=targetY-monsterYPos;
       monsterYPos+=dy*monsterRate;


       if(dx>veiwSize||dx<-veiwSize)
       {
         InSight=false;
       }

       if(dy>veiwSize||dy<-veiwSize)
       {
         InSight=false;
       }
     }


     if(InSight==false)
     {
       if(monsterZPos>monsterXPos)
       {
         monsterXPos++;
       }
       if(monsterZPos<monsterXPos)
       {
         monsterXPos--;
       }
       if(monsterZPos==monsterXPos)
       {
         monsterZPos=int(random(width-100));
       } 
     }


    if(spriteXPos+Width>monsterXPos && spriteXPos<monsterXPos+Width)
    {
      if(spriteYPos+Height>monsterYPos && spriteYPos<monsterYPos+Height)
      {
        Collision=true;
      }
    }

    if(Collision==true)
    {

        if(second==1)
        {
          health--;
          second=0;

          if(health<0)
          {
              health=0; 
          }
          Collision=false;
        }
      }
    }



  void  Draw()
  {
    for(int i=0;i<monsters.length;i++)
    {
      if(monsterWVal==0)
      {
        image(monster1,monsterXPos,monsterYPos);
      }
      if(monsterWVal==1)
      {
        image(monster2,monsterXPos,monsterYPos);
      }
      if(monsterWVal==2)
      {
        image(monster3,monsterXPos,monsterYPos);
      }
      if(monsterWVal==3)
      {
        image(monster4,monsterXPos,monsterYPos);
      }
    }
  }
}
\$\endgroup\$
  • \$\begingroup\$ When posting a question there's a button labeled {}. Highlight your code and press it to keep identations \$\endgroup\$ – Bálint Apr 20 at 15:30
  • \$\begingroup\$ What is the purpose of if(second==1) in the collision detection? I notice second is getting incremented on every 30th draw, so it's likely to be greater than 1 at some point, meaning the body of the if will never execute. \$\endgroup\$ – user1118321 Apr 20 at 16:06
  • \$\begingroup\$ @user1118321 The timing system that you are seeing is to allow for a time delay between the collision and the damage taken from the player by a monster. This it to prevent the monster from killing the player before the player can do anything. I think I can see what you are saying, however, so I’ll try to make a fix for it \$\endgroup\$ – lockheed silverman Apr 20 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.