/* object-collision OBJID_PLAYER against OBJID_GD_HEART, OBJID_GD_KEY, OBJID_GD_PILL, OBJID_GD_KEYHOLE, OBJID_GD_BOMBS, OBJID_GD_GOLD */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include "obj-player.h"
#include "obj-gd_heart.h"
#include "obj-gd_key.h"
#include "obj-gd_pill.h"
#include "obj-gd_keyhole.h"
#include "obj-gd_bombs.h"
#include "obj-gd_gold.h"
#include "obj-audio.h"

int objcoll_PLAYER_obj2(void *, unsigned int, struct VG_Coll *);


int
objcoll_PLAYER_obj2(void *vgame, unsigned int instanceid, struct VG_Coll *collb)
{
  struct s_game *sgame = (struct s_game *)vgame;
  struct VG_Object *objp;
  struct sobj_audio *objvars_audio;
  struct sobj_player *objvars_player;
  int retw = VG_COLL_RETURN_CONTINUE;

  if (sgame == NULL) { return retw; }

  if (collb->type != VG_COLL_TYPE_ENTRY) { return retw; }

  /* audio instance */
  objp = vg4->object->instance_getobj(sgame->instanceid.audio);
  objvars_audio = (struct sobj_audio *)objp->opriv;

  /* "moving" instance must be OBJID_PLAYER */
  objp = vg4->object->instance_getobj(instanceid);
  if (strcmp(objp->objid, OBJID_PLAYER) != 0) { return retw; }
  objvars_player = (struct sobj_player *)objp->opriv;

  /* collided object-instance */

  objp = vg4->object->instance_getobj(collb->instanceid);

  if (strcmp(objp->objid, OBJID_GD_HEART) == 0) {
    if (objvars_player->extralife == 0) {
      objvars_player->extralife = 1;
      vg4->object->destroy(sgame, collb->instanceid);
      vg4->audio->play(objvars_audio->a_heart, VG_FALSE, VG_FALSE);
    }

  } else if (strcmp(objp->objid, OBJID_GD_KEY) == 0) {
    objvars_player->has_key = VG_TRUE;
    vg4->object->destroy(sgame, collb->instanceid);
    vg4->audio->play(objvars_audio->a_key, VG_FALSE, VG_FALSE);

  } else if (strcmp(objp->objid, OBJID_GD_PILL) == 0) {
    objvars_player->dark_counter = 1000 / LOOP_TIME * 20;
    vg4->object->destroy(sgame, collb->instanceid);
    vg4->audio->play(objvars_audio->a_pill, VG_FALSE, VG_FALSE);

  } else if (strcmp(objp->objid, OBJID_GD_KEYHOLE) == 0) {
    if (objvars_player->has_key) {
      vg4->object->destroy(sgame, collb->instanceid);
      objvars_player->has_key = VG_FALSE;
      /* open OBJID_EXIT */
      vg4->object->call_signal(sgame, sgame->instanceid.exit, SIGNO_ACTIVE);
      vg4->audio->play(objvars_audio->a_keyhole, VG_FALSE, VG_FALSE);
    }

  } else if (strcmp(objp->objid, OBJID_GD_BOMBS) == 0) {
    objvars_player->anzbombs++;
    vg4->object->destroy(sgame, collb->instanceid);
    vg4->audio->play(objvars_audio->a_bombs, VG_FALSE, VG_FALSE);

  } else if (strcmp(objp->objid, OBJID_GD_GOLD) == 0) {
    struct sobj_gd_gold *objvars = (struct sobj_gd_gold *)objp->opriv;
    objvars_player->gold += objvars->gtype;
    vg4->object->destroy(sgame, collb->instanceid);
    vg4->audio->play(objvars_audio->a_gold, VG_FALSE, VG_FALSE);
  }

  return retw;
}
