global.customobject[0] = object_add() //the weapon itself.
ak47sprite = sprite_add(datadir+"ak47.png",0,false,false,0,0)
object_set_sprite(global.customobject[0],ak47sprite)
global.customobject[1] = object_add() //the bullet.
sprite = sprite_add(datadir+"spr_bullet.png",0,false,false,0,0)
object_set_sprite(global.customobject[1],sprite)
global.customobject[2] = object_add() //the pickup object of the weapon.
object_set_sprite(global.customobject[2],ak47sprite)
global.customobject[3] = object_add() //gunfire effect just like in teeworlds.
global.gunfire1 = sprite_add(datadir+"spr_gunfire1.png",0,false,false,0,0)
sprite_set_offset(global.gunfire1,sprite_get_width(global.gunfire1)-(sprite_get_width(global.gunfire1)*1.5),sprite_get_height(global.gunfire1)/2)
global.gunfire2 = sprite_add(datadir+"spr_gunfire2.png",0,false,false,0,0)
sprite_set_offset(global.gunfire2,sprite_get_width(global.gunfire2)-(sprite_get_width(global.gunfire2)*1.5),sprite_get_height(global.gunfire2)/2)
object_set_sprite(global.customobject[3],global.gunfire1)
global.customobject[4] = object_add() //weapon chooser for more than 1 weapon.
global.ak47ammo = 0
global.pistolammo = 0
global.ak47pickedup = 0
global.pistolpickedup = 0
global.currweapon = 0
global.reload = 0
global.reloading = 1
global.spread  = 1
object_event_add(global.customobject[4],ev_create,0,"global.ak47pickedup = 0; global.pistolpickedup = 0; global.ak47ammo = 0; global.pistolammo = 0;")
object_event_add(global.customobject[4],ev_keypress,ord("1"),"if global.ak47pickedup = 1 && global.currweapon != global.customobject[0] {with global.currweapon instance_change(global.customobject[0],true); global.reload = 0; global.reloading = 0; global.currweapon = global.customobject[0]; global.pistolammo = global.ammo; global.ammo = global.ak47ammo; sound_play(choose(snd_switch01,snd_switch02,snd_switch03))}")
object_event_add(global.customobject[4],ev_keypress,ord("2"),"if global.pistolpickedup = 1 && global.currweapon != global.customobject[6] {with global.currweapon instance_change(global.customobject[6],true); global.reload = 0; global.reloading = 0; global.currweapon = global.customobject[6]; global.ak47ammo = global.ammo; global.ammo = global.pistolammo; sound_play(choose(snd_switch01,snd_switch02,snd_switch03))}")
object_event_add(global.customobject[4],ev_keypress,ord("E"),"if global.currweapon != 0 {global.reloading = global.currweapon; if global.reloading = global.customobject[0] then sound_play(global.ak47reload); if global.reloading = global.customobject[6] then sound_play(global.deaglereload);}")
object_event_add(global.customobject[4],ev_step,ev_step_normal,"if global.reloading = global.currweapon then global.reload += 1.5; if global.reload >= 100 then {if global.currweapon = global.customobject[0] then global.ammo = 30; else if global.currweapon = global.customobject[6] then global.ammo = 7; global.reloading = 0; global.reload = 0} if global.spread > 1 then global.spread -= 0.125")
object_event_add(global.customobject[4],ev_draw,0,"if global.reloading = global.currweapon then draw_healthbar(localPlayer.x-40,localPlayer.y-40,localPlayer.x+40,localPlayer.y-32,global.reload,c_black,c_red,c_yellow,0,true,true); draw_text(64,64,'press 1 or 2 to switch weapons#press E to reload#collecting weapons also reloads but immediately')")

sprite = sprite_add(datadir+"spr_deagle.png",0,false,false,0,0)
global.customobject[5] = object_add() //the pickup object of the pistol
global.customobject[6] = object_add() //the pistol
object_set_sprite(global.customobject[5],sprite)
object_set_sprite(global.customobject[6],sprite)
object_set_sprite(global.customobject[4],spr_solid)

weaponIcon = sprite_add(datadir+"spr_ak47_ammo.png",0,false,false,0,0)
object_set_depth(global.customobject[0],5000)
object_set_depth(global.customobject[6],5000)
object_event_add(global.customobject[3],ev_create,0,"sprite_index = choose(global.gunfire1,global.gunfire2); alarm[0] = 1")
object_event_add(global.customobject[3],ev_alarm,0,"instance_destroy()")
object_event_add(global.customobject[2],ev_collision,localPlayer,"sound_play(global.ak47pickup); global.ak47ammo = 30; if global.ak47pickedup = false && global.pistolpickedup = false then {instance_create(0,0,global.customobject[0]); global.ak47pickedup = true; global.ammo = global.ak47ammo; global.currweapon = global.customobject[0];} else if global.ak47pickedup = false then global.ak47pickedup = true; if global.currweapon = global.customobject[0] then global.ammo = 30; instance_destroy()")
object_event_add(global.customobject[5],ev_collision,localPlayer,"sound_play(global.ak47pickup); global.pistolammo = 7; if global.pistolpickedup = false && global.ak47pickedup = false then {instance_create(0,0,global.customobject[6]); global.pistolpickedup = true; global.ammo = global.pistolammo; global.currweapon = global.customobject[6];} else if global.pistolpickedup = false then global.pistolpickedup = true; if global.currweapon = global.customobject[6] then global.ammo = 7; instance_destroy()")
object_event_add(global.customobject[0],ev_create,0,"can_fire = 1")
object_event_add(global.customobject[0],ev_alarm,0,"can_fire = 1")
object_event_add(global.customobject[6],ev_create,0,"can_fire = 1")
object_event_add(global.customobject[6],ev_alarm,0,"can_fire = 1")
object_event_add(global.customobject[1],ev_collision,obj_solid,"instance_destroy()")
object_event_add(global.customobject[0],ev_step,ev_step_end,"
//find direction toward mouse, based on player
dir = point_direction(localPlayer.x,localPlayer.y,mouse_x,mouse_y)
image_angle=dir

//flip gun sprite and make gun follow
//player's position
if localPlayer.x < mouse_x
{
    image_yscale=+1
    x=localPlayer.x+0
    y=localPlayer.y+0
    }
else
{
    image_yscale=-1
    x=localPlayer.x-0
    y=localPlayer.y+0
    }")
object_event_add(global.customobject[6],ev_step,ev_step_end,"
//find direction toward mouse, based on player
dir = point_direction(localPlayer.x,localPlayer.y,mouse_x,mouse_y)
image_angle=dir

//flip gun sprite and make gun follow
//player's position
if localPlayer.x < mouse_x
{
    image_yscale=+1
    x=localPlayer.x+0
    y=localPlayer.y+0
    }
else
{
    image_yscale=-1
    x=localPlayer.x-0
    y=localPlayer.y+0
    }")

object_event_add(global.customobject[0],ev_step,ev_step_normal,"
if mouse_check_button(global.fire)
{
    if global.ammo > 0 && can_fire = 1 && global.reload = 0
    {
        var xp, yp;
        xp=x+30*cos( degtorad(image_angle) )
        yp=y-30*sin( degtorad(image_angle) )
		bob=instance_create(xp,yp,global.customobject[1])
		sound_play(global.ak47)
		gunfire = instance_create(xp,yp,global.customobject[3])
		gunfire.image_angle = image_angle
		gunfire.direction = gunfire.image_angle
        bob.speed=75
        bob.direction=image_angle+irandom_range(-global.spread,global.spread)
        bob.image_angle=bob.direction
        if isMultiplayer = 1 then bob.pid = obj_client.player_id
        global.ammo -= 1
		global.ak47ammo -= 1
        can_fire = 0
        alarm[0] = 4
		global.spread += 1
        if isMultiplayer = 1 then net_client_send(OTHERBULLET,9,NET_INT,obj_client.player_id,global.customobject[1],bob.x,bob.y,bob.direction,bob.image_angle,bob.speed,bob.gravity,round(random(500)))
    }
    else
    {
        if global.ammo = 0
        {
        var clicksound;
clicksound = choose(1,2,3,4,5);
switch (clicksound)
{
case 1:
sound_play(snd_wpnclick1);
break;
case 2:
sound_play(snd_wpnclick2);
break;
case 3:
sound_play(snd_wpnclick3);
break;
case 4:
sound_play(snd_wpnclick4);
break;
case 5:
sound_play(snd_wpnclick5);
break;
}
}
    }
}")
object_event_add(global.customobject[6],ev_step,ev_step_normal,"
if mouse_check_button_pressed(global.fire)
{
    if global.ammo > 0 && can_fire = 1 && global.reload = 0
    {
        var xp, yp;
        xp=x+30*cos( degtorad(image_angle) )
        yp=y-30*sin( degtorad(image_angle) )
		bob=instance_create(xp,yp,global.customobject[1])
		sound_play(global.deagle)
		gunfire = instance_create(xp,yp,global.customobject[3])
		gunfire.image_angle = image_angle
		gunfire.direction = gunfire.image_angle
        bob.speed=75
        bob.direction=image_angle+irandom_range(-global.spread,global.spread)
        bob.image_angle=bob.direction
        if isMultiplayer = 1 then bob.pid = obj_client.player_id
        global.ammo -= 1
		global.pistolammo -= 1
        can_fire = 0
        alarm[0] = 9
		global.spread += 1
        if isMultiplayer = 1 then net_client_send(OTHERBULLET,9,NET_INT,obj_client.player_id,global.customobject[1],bob.x,bob.y,bob.direction,bob.image_angle,bob.speed,bob.gravity,round(random(500)))
    }
    else
    {
        if global.ammo = 0
        {
        var clicksound;
clicksound = choose(1,2,3,4,5);
switch (clicksound)
{
case 1:
sound_play(snd_wpnclick1);
break;
case 2:
sound_play(snd_wpnclick2);
break;
case 3:
sound_play(snd_wpnclick3);
break;
case 4:
sound_play(snd_wpnclick4);
break;
case 5:
sound_play(snd_wpnclick5);
break;
}
}
    }
}")

global.ak47pickup = sound_add(sounddir+"ak47-pickup.wav",0,true)
global.ak47 = sound_add(sounddir+"ak47-1.wav",0,true)
global.deagle = sound_add(sounddir+"deagle-1.wav",0,true)
global.ak47reload = sound_add(sounddir+"ak47-reload.wav",0,true)
global.deaglereload = sound_add(sounddir+"deagle-reload.wav",0,true)
global.imgs = 0
global.objs = 7
