It's supposed to shoot the player in a mission.
Script
Code tag doesn't work has written
--pistol soldier script made by Loooser--
if loooser==nil then loooser={} end
cc.enemy_rifle={}
cc.enemy_rifle.object={}
cc.enemy_rifle.bullet={}
cc.enemy_rifle.gfx_wpn={}
cc.enemy_rifle.sfx_bounce={}
--Loading images and sounds
cc.enemy_rifle.gfx_wpn=loadgfx("ZJ/enemy_armed.png")
setmidhandle(cc.enemy_rifle.gfx_wpn)
cc.enemy_rifle.gfx_wpn2=loadgfx("ZJ/rifle.bmp")
setmidhandle(cc.enemy_rifle.gfx_wpn2)
for i = 0,3 do
cc.enemy_rifle.sfx_bounce[i]=loadsfx("fall"..i..".wav")
end
cc.enemy_rifle.sfx_attack=loadsfx("rifle.wav")
cc.enemy_rifle.gfx_pro=loadgfx("weapons/shot.bmp") -- Projectile Image
setmidhandle(cc.enemy_rifle.gfx_pro)
--------------------------------------------------------------------------
--Object: soldier pistol
--------------------------------------------------------------------------
cc.enemy_rifle.object.id=addobject("cc.enemy_rifle.object",cc.enemy_rifle.gfx_wpn)
function cc.enemy_rifle.object.setup(id,parameter)
if objects==nil then objects={} end
objects[id]={}
objects[id].health=50
objects[id].sy=0
objects[id].wpn_gfx=cc.enemy_rifle.gfx_wpn2
objects[id].rot=300
objects[id].ldir=1
objects[id].player=playercurrent()
objects[id].timer=0
objects[id].ammo=1
end
function cc.enemy_rifle.object.draw(id,x,y)
-- Setup draw mode
setblend(blend_alpha)
setalpha(1)
setcolor(255,255,255)
if math.sin(math.rad(objects[id].rot))>0 then
setscale(1,1)
else
setscale(-1,1)
end
setrotation(0)
-- Draw
drawimage(cc.enemy_rifle.gfx_wpn,x,y)
setblend(blend_alpha)
setalpha(1)
setcolor(255,255,255)
if math.sin(math.rad(objects[id].rot))>0 then
setscale(1,1)
else
setscale(-1,1)
end
setrotation(objects[id].rot)
drawimage(objects[id].wpn_gfx,x,y)
end
function cc.enemy_rifle.object.update(id,x,y)
--if player hp<0 then kill
if objects[id].health<=0 then
particle(p_bodypart,x,y)
particlecolor(0,255,0)
for i = 1,3 do
particle(p_bigblood,x,y)
end
removeobject(id)
return
end
objects[id].sy=objects[id].sy+getgravity()
msubt=math.ceil(math.abs(objects[id].sy)/15)
msuby=objects[id].sy/msubt
for i=1,msubt,1 do
y=y+msuby
if collision(colplayer,x,y,1,1,1,-1,id)==1 then
if (math.abs(objects[id].sy)>0.5) then playsound(cc.enemy_rifle.sfx_bounce[math.random(0,3)]) end
y=y-msuby
objects[id].sy=-objects[id].sy*0.3
msuby=-msuby*0.3
end
-- Water
if y>getwatery()+5 then
-- Effects
particle(p_waterhit,x,y)
playsound(sfx_hitwater1)
-- Remove
removeobject(id)
return
end
end
objects[id].timer=objects[id].timer-1
objectposition(id,x,y)
local players=playertable(0,0)
for i=1,#allobjects do
if objects[allobjects[i]]~=nil and objects[allobjects[i]].brate~=nil and objects[allobjects[i]].exists~=nil then -- find heli
if objects[allobjects[i]].exists==100 then
local dist=((x-getplayerx(players[i]))^2+(y-getplayery(players[i]))^2)^0.5
if dist<(screenwidth()/2) then
aimangleneeded=math.deg(math.atan2(getobjectx(allobjects[i])-x,-(getobjecty(allobjects[i])-y)))
objects[id].rot=objects[id].rot+angledelta(objects[id].rot,aimangleneeded)*0.8
if math.abs(angledelta(objects[id].rot,aimangleneeded))<10 then
--right direction
local ok=1
if ok==1 and objects[id].timer<=0 then
playsound(cc.enemy_rifle.sfx_attack)
objects[id].ammo=objects[id].ammo-1
--create projectile
particle(p_muzzle,x+math.sin(math.rad(objects[id].rot))*16,y+3-math.cos(math.rad(objects[id].rot))*16)
particle(p_smoke,x+math.sin(math.rad(objects[id].rot))*16,y+3-math.cos(math.rad(objects[id].rot))*16)
particlespeed(-0.2+math.random()*0.4+getwind()*10.0,-1.0+math.random()*0.6)
particlefadealpha(0.005)
particle(p_bullet,x,y+3)
if objects[id].ammo==0 then
objects[id].timer=13
objects[id].ammo=1
else
objects[id].timer=13
end
bid=createprojectile(cc.enemy_rifle.bullet.id)
projectiles[bid]={}
-- Ignore collision with current player at beginning
projectiles[bid].ignore=id
-- Set initial position of projectile
projectiles[bid].x=x-math.sin(math.rad(objects[id].rot))*5.0
projectiles[bid].y=y+3+math.cos(math.rad(objects[id].rot))*5.0
-- Set speed of projectile
projectiles[bid].sx=math.sin(math.rad(objects[id].rot))*16.0
projectiles[bid].sy=-math.cos(math.rad(objects[id].rot))*16.0
projectiles[bid].countdown=100
-- Initial movement
projectiles[bid].x=projectiles[bid].x-projectiles[bid].sx*1.5
projectiles[bid].y=projectiles[bid].y-projectiles[bid].sy*1.5
for i=1,3,1 do
if cc.enemy_rifle.bullet.move(bid)==1 then
break
end
end
end
end
end
end
end
end
end
function cc.enemy_rifle.object.damage(id,damage)
objects[id].health=objects[id].health-damage
particle(p_bigblood,getobjectx(id),getobjecty(id))
end
--------------------------------------------------------------------------------
-- Projectile: Bullet
--------------------------------------------------------------------------------
cc.enemy_rifle.bullet.id=addprojectile("cc.enemy_rifle.bullet") -- Add Projectile
function cc.enemy_rifle.bullet.draw(id) -- Draw
-- Setup draw mode
setblend(blend_light)
setalpha(1)
setcolor(255,255,0)
setscale(1,1)
-- Calculate projectile rotation
setrotation(math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy)))
-- Draw projectile
drawimage(cc.enemy_rifle.gfx_pro,projectiles[id].x,projectiles[id].y)
-- Draw Arrow if out of Screen
outofscreenarrow(projectiles[id].x,projectiles[id].y)
end
function cc.enemy_rifle.bullet.update(id) -- Update
-- Wind + Gravity influence on speed
projectiles[id].sx=projectiles[id].sx+getwind()*0.009
projectiles[id].sy=projectiles[id].sy+getgravity()*0.65
-- Move
cc.enemy_rifle.bullet.move(id)
end
function cc.enemy_rifle.bullet.move(id)
rot=math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy))
-- Move (in substep loop for optimal collision precision)
msubt=math.ceil(math.max(math.abs(projectiles[id].sx),math.abs(projectiles[id].sy))/3)
msubx=projectiles[id].sx/msubt
msuby=projectiles[id].sy/msubt
for i=1,msubt,1 do
projectiles[id].x=projectiles[id].x+msubx
projectiles[id].y=projectiles[id].y+msuby
-- Collision
if collision(col3x3,projectiles[id].x+math.sin(math.rad(rot))*20,projectiles[id].y-math.cos(math.rad(rot))*20,1,1,1,-1,projectiles[id].ignore)==1 then
-- Cause damage
if playercollision()~=0 then
playerpush(playercollision(),projectiles[id].sx/10.0,projectiles[id].sy/10.0)
playerdamage(playercollision(),32)
blood(projectiles[id].x+math.sin(math.rad(rot))*20,projectiles[id].y-math.cos(math.rad(rot))*20)
elseif objectcollision()>0 then
objectdamage(objectcollision(),32)
end
-- Effects
playsound(sfx_ricochet1)
particle(p_smoke,projectiles[id].x+math.sin(math.rad(rot))*21,projectiles[id].y-math.cos(math.rad(rot))*21)
particlefadealpha(0.006)
particle(p_muzzle,projectiles[id].x+math.sin(math.rad(rot))*21,projectiles[id].y-math.cos(math.rad(rot))*21)
-- Free projectile
freeprojectile(id)
return 1
end
-- Water
if (projectiles[id].y)>getwatery()+5 then
-- Effects
particle(p_waterhit,projectiles[id].x,projectiles[id].y)
if math.random(1,2)==1 then
playsound(sfx_hitwater2)
else
playsound(sfx_hitwater3)
end
-- Free projectile
freeprojectile(id)
return 1
end
end
end
if loooser==nil then loooser={} end
cc.enemy_rifle={}
cc.enemy_rifle.object={}
cc.enemy_rifle.bullet={}
cc.enemy_rifle.gfx_wpn={}
cc.enemy_rifle.sfx_bounce={}
--Loading images and sounds
cc.enemy_rifle.gfx_wpn=loadgfx("ZJ/enemy_armed.png")
setmidhandle(cc.enemy_rifle.gfx_wpn)
cc.enemy_rifle.gfx_wpn2=loadgfx("ZJ/rifle.bmp")
setmidhandle(cc.enemy_rifle.gfx_wpn2)
for i = 0,3 do
cc.enemy_rifle.sfx_bounce[i]=loadsfx("fall"..i..".wav")
end
cc.enemy_rifle.sfx_attack=loadsfx("rifle.wav")
cc.enemy_rifle.gfx_pro=loadgfx("weapons/shot.bmp") -- Projectile Image
setmidhandle(cc.enemy_rifle.gfx_pro)
--------------------------------------------------------------------------
--Object: soldier pistol
--------------------------------------------------------------------------
cc.enemy_rifle.object.id=addobject("cc.enemy_rifle.object",cc.enemy_rifle.gfx_wpn)
function cc.enemy_rifle.object.setup(id,parameter)
if objects==nil then objects={} end
objects[id]={}
objects[id].health=50
objects[id].sy=0
objects[id].wpn_gfx=cc.enemy_rifle.gfx_wpn2
objects[id].rot=300
objects[id].ldir=1
objects[id].player=playercurrent()
objects[id].timer=0
objects[id].ammo=1
end
function cc.enemy_rifle.object.draw(id,x,y)
-- Setup draw mode
setblend(blend_alpha)
setalpha(1)
setcolor(255,255,255)
if math.sin(math.rad(objects[id].rot))>0 then
setscale(1,1)
else
setscale(-1,1)
end
setrotation(0)
-- Draw
drawimage(cc.enemy_rifle.gfx_wpn,x,y)
setblend(blend_alpha)
setalpha(1)
setcolor(255,255,255)
if math.sin(math.rad(objects[id].rot))>0 then
setscale(1,1)
else
setscale(-1,1)
end
setrotation(objects[id].rot)
drawimage(objects[id].wpn_gfx,x,y)
end
function cc.enemy_rifle.object.update(id,x,y)
--if player hp<0 then kill
if objects[id].health<=0 then
particle(p_bodypart,x,y)
particlecolor(0,255,0)
for i = 1,3 do
particle(p_bigblood,x,y)
end
removeobject(id)
return
end
objects[id].sy=objects[id].sy+getgravity()
msubt=math.ceil(math.abs(objects[id].sy)/15)
msuby=objects[id].sy/msubt
for i=1,msubt,1 do
y=y+msuby
if collision(colplayer,x,y,1,1,1,-1,id)==1 then
if (math.abs(objects[id].sy)>0.5) then playsound(cc.enemy_rifle.sfx_bounce[math.random(0,3)]) end
y=y-msuby
objects[id].sy=-objects[id].sy*0.3
msuby=-msuby*0.3
end
-- Water
if y>getwatery()+5 then
-- Effects
particle(p_waterhit,x,y)
playsound(sfx_hitwater1)
-- Remove
removeobject(id)
return
end
end
objects[id].timer=objects[id].timer-1
objectposition(id,x,y)
local players=playertable(0,0)
for i=1,#allobjects do
if objects[allobjects[i]]~=nil and objects[allobjects[i]].brate~=nil and objects[allobjects[i]].exists~=nil then -- find heli
if objects[allobjects[i]].exists==100 then
local dist=((x-getplayerx(players[i]))^2+(y-getplayery(players[i]))^2)^0.5
if dist<(screenwidth()/2) then
aimangleneeded=math.deg(math.atan2(getobjectx(allobjects[i])-x,-(getobjecty(allobjects[i])-y)))
objects[id].rot=objects[id].rot+angledelta(objects[id].rot,aimangleneeded)*0.8
if math.abs(angledelta(objects[id].rot,aimangleneeded))<10 then
--right direction
local ok=1
if ok==1 and objects[id].timer<=0 then
playsound(cc.enemy_rifle.sfx_attack)
objects[id].ammo=objects[id].ammo-1
--create projectile
particle(p_muzzle,x+math.sin(math.rad(objects[id].rot))*16,y+3-math.cos(math.rad(objects[id].rot))*16)
particle(p_smoke,x+math.sin(math.rad(objects[id].rot))*16,y+3-math.cos(math.rad(objects[id].rot))*16)
particlespeed(-0.2+math.random()*0.4+getwind()*10.0,-1.0+math.random()*0.6)
particlefadealpha(0.005)
particle(p_bullet,x,y+3)
if objects[id].ammo==0 then
objects[id].timer=13
objects[id].ammo=1
else
objects[id].timer=13
end
bid=createprojectile(cc.enemy_rifle.bullet.id)
projectiles[bid]={}
-- Ignore collision with current player at beginning
projectiles[bid].ignore=id
-- Set initial position of projectile
projectiles[bid].x=x-math.sin(math.rad(objects[id].rot))*5.0
projectiles[bid].y=y+3+math.cos(math.rad(objects[id].rot))*5.0
-- Set speed of projectile
projectiles[bid].sx=math.sin(math.rad(objects[id].rot))*16.0
projectiles[bid].sy=-math.cos(math.rad(objects[id].rot))*16.0
projectiles[bid].countdown=100
-- Initial movement
projectiles[bid].x=projectiles[bid].x-projectiles[bid].sx*1.5
projectiles[bid].y=projectiles[bid].y-projectiles[bid].sy*1.5
for i=1,3,1 do
if cc.enemy_rifle.bullet.move(bid)==1 then
break
end
end
end
end
end
end
end
end
end
function cc.enemy_rifle.object.damage(id,damage)
objects[id].health=objects[id].health-damage
particle(p_bigblood,getobjectx(id),getobjecty(id))
end
--------------------------------------------------------------------------------
-- Projectile: Bullet
--------------------------------------------------------------------------------
cc.enemy_rifle.bullet.id=addprojectile("cc.enemy_rifle.bullet") -- Add Projectile
function cc.enemy_rifle.bullet.draw(id) -- Draw
-- Setup draw mode
setblend(blend_light)
setalpha(1)
setcolor(255,255,0)
setscale(1,1)
-- Calculate projectile rotation
setrotation(math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy)))
-- Draw projectile
drawimage(cc.enemy_rifle.gfx_pro,projectiles[id].x,projectiles[id].y)
-- Draw Arrow if out of Screen
outofscreenarrow(projectiles[id].x,projectiles[id].y)
end
function cc.enemy_rifle.bullet.update(id) -- Update
-- Wind + Gravity influence on speed
projectiles[id].sx=projectiles[id].sx+getwind()*0.009
projectiles[id].sy=projectiles[id].sy+getgravity()*0.65
-- Move
cc.enemy_rifle.bullet.move(id)
end
function cc.enemy_rifle.bullet.move(id)
rot=math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy))
-- Move (in substep loop for optimal collision precision)
msubt=math.ceil(math.max(math.abs(projectiles[id].sx),math.abs(projectiles[id].sy))/3)
msubx=projectiles[id].sx/msubt
msuby=projectiles[id].sy/msubt
for i=1,msubt,1 do
projectiles[id].x=projectiles[id].x+msubx
projectiles[id].y=projectiles[id].y+msuby
-- Collision
if collision(col3x3,projectiles[id].x+math.sin(math.rad(rot))*20,projectiles[id].y-math.cos(math.rad(rot))*20,1,1,1,-1,projectiles[id].ignore)==1 then
-- Cause damage
if playercollision()~=0 then
playerpush(playercollision(),projectiles[id].sx/10.0,projectiles[id].sy/10.0)
playerdamage(playercollision(),32)
blood(projectiles[id].x+math.sin(math.rad(rot))*20,projectiles[id].y-math.cos(math.rad(rot))*20)
elseif objectcollision()>0 then
objectdamage(objectcollision(),32)
end
-- Effects
playsound(sfx_ricochet1)
particle(p_smoke,projectiles[id].x+math.sin(math.rad(rot))*21,projectiles[id].y-math.cos(math.rad(rot))*21)
particlefadealpha(0.006)
particle(p_muzzle,projectiles[id].x+math.sin(math.rad(rot))*21,projectiles[id].y-math.cos(math.rad(rot))*21)
-- Free projectile
freeprojectile(id)
return 1
end
-- Water
if (projectiles[id].y)>getwatery()+5 then
-- Effects
particle(p_waterhit,projectiles[id].x,projectiles[id].y)
if math.random(1,2)==1 then
playsound(sfx_hitwater2)
else
playsound(sfx_hitwater3)
end
-- Free projectile
freeprojectile(id)
return 1
end
end
end