chronic 4 gamemaker team


    Basic turret engine

    Share
    avatar
    Jimmynator
    Admin

    Posts : 71
    Join date : 2010-03-30
    Age : 22
    Location : Spain

    Basic turret engine

    Post  Jimmynator on Thu Apr 01, 2010 2:14 pm

    This is a basic turrret example:

    DOWNLOAD NOW

    First we have to create 5 sprites:

    spr_turret
    spr_enemy
    spr_obstacle
    spr_bullet
    spr_assistant

    spr_assistant is the vision range for the get_vision script that I made. We are doing a turret, so it has to be a line. In this example it is 200 x 1.

    Now we have to create 3 scripts:

    get_vision:
    Code:
    /*
    argument0 = assistant object
    argument1 = enemy object
    */
    if !place_meeting(x,y,argument0)
    {
    instance_create(x,y,argument0)
    exit
    }
    if instance_nearest(x,y,argument0).coll = 0
    {
    if instance_nearest(x,y,argument0).comy = 1
    {
    return true
    }
    else
    {
    return false
    }
    }
    else
    {
    return false
    }

    as_step (place in the step event of the assistant object):
    Code:
    /*
    argument0 = object to assist
    argument1 = obstacle object
    argument2 = range sprite
    argument3 = enemy object
    */
    if !place_meeting(x,y,argument3)
    comy=0;
    if place_meeting(x,y,argument3)
    comy=1;
    if !place_meeting(x,y,argument1)
    coll=0;
    sprite_index = argument2
    image_angle=instance_nearest(x,y,argument0).image_angle
    x=instance_nearest(x,y,argument0).x
    y=instance_nearest(x,y,argument0).y

    as_collision (place in the collision event with the obstacle object):
    Code:
    /*
    argument0 = enemy object
    argument1 = obstacle object
    */
    if collision_line(x,y,argument0.x,argument0.y,argument1,false,false)
    coll = 1;
    else
    coll = 0;

    Now the objects:

    obj_turret (step event):
    Use any sprite.
    We have to change its image_angle so that points to the enemy object. We will use this function:
    Code:
    point_direction(x,y,instance_nearest(x,y,obj_enemy).x,instance_nearest(x,y,obj_enemy).y)
    Now we have to see if the enemy is in the line of sight, so we will use the get_vision sript:
    Code:
    if get_vision(obj_assistant,obj_enemy)
    If it is true it will shoot:
    Code:
    {
    bullet = instance_create(x,y,obj_bullet)
    bullet.direction = image_angle
    bullet.speed = 10
    }

    obj_enemy:
    Use any sprite. If you want, you can make it move.

    obj_assistant:
    Use any sprite. Make it invisible.
    In the step event we will paste this line of code:
    Code:
    ast_step(obj_turret,obj_obstacle,spr_assistant,obj_enemy)
    In the collision event with the enemy object paste this line of code:
    Code:
    as_collision(obj_enemy,obj_obstacle)

    obj_obstacle:
    Use any sprite.

    obj_bullet:
    Use any sprite.
    Step event:
    Code:
    image_angle = direction
    collision event with enemy, collision event with obstacle and outside room:
    Code:
    instance_destroy()

    Smile Create a room and try it Smile


    Last edited by Jimmynator on Fri Apr 02, 2010 12:43 pm; edited 4 times in total

    Flyboymonkey
    Admin

    Posts : 93
    Join date : 2010-03-29
    Age : 36

    Re: Basic turret engine

    Post  Flyboymonkey on Thu Apr 01, 2010 4:37 pm

    Sorry if I didn't read it properly but I cant see right with my allergies. Did you do anything like:
    //-------------------setting the target----------------------
    target = -1;
    nearDist = point_distance(0,0,aggro_range,aggro_range);

    with(ally_parent) {
    myDist = point_distance(x,y,other.x,other.y);
    if( myDist < other.nearDist and !collision_line(x,y,other.x,other.y,wall,true,true)) {
    other.target = id;
    other.nearDist = myDist;
    }
    }

    To make sure it doesnt flippity flop?

    Just checking Razz
    avatar
    Jimmynator
    Admin

    Posts : 71
    Join date : 2010-03-30
    Age : 22
    Location : Spain

    Re: Basic turret engine

    Post  Jimmynator on Fri Apr 02, 2010 12:42 pm

    Try it. I've uploaded an example.
    avatar
    Jimmynator
    Admin

    Posts : 71
    Join date : 2010-03-30
    Age : 22
    Location : Spain

    Re: Basic turret engine

    Post  Jimmynator on Sat Apr 03, 2010 8:11 am

    Has anyone tried the example?
    avatar
    Jimmynator
    Admin

    Posts : 71
    Join date : 2010-03-30
    Age : 22
    Location : Spain

    Re: Basic turret engine

    Post  Jimmynator on Sat Apr 03, 2010 4:58 pm

    Please, reply.

    Flyboymonkey
    Admin

    Posts : 93
    Join date : 2010-03-29
    Age : 36

    Re: Basic turret engine

    Post  Flyboymonkey on Sat Apr 03, 2010 5:49 pm

    I love the triangle example.. very good idea ^^
    avatar
    Jimmynator
    Admin

    Posts : 71
    Join date : 2010-03-30
    Age : 22
    Location : Spain

    Re: Basic turret engine

    Post  Jimmynator on Sat Apr 03, 2010 5:52 pm

    Thanks. It produces very little lag.
    You can make a custom vision sprite, like a square. Very Happy
    avatar
    xxjacik5xa

    Posts : 30
    Join date : 2010-04-04

    Re: Basic turret engine

    Post  xxjacik5xa on Sat Apr 10, 2010 1:13 pm

    wow this is amazing Razz

    Sponsored content

    Re: Basic turret engine

    Post  Sponsored content


      Current date/time is Wed Jul 18, 2018 9:35 pm