Berserk88

scr_spawn_ship

Aug 11th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. /// @description scr_spawn_ship(x, y, blueprint, ship sprite)
  2. /// @param x
  3. /// @param y
  4. /// @param blueprint
  5. /// @param ship sprite
  6. //
  7. // Spawn ai controlled ship
  8. // this code doesn't allow for components to be customized
  9. // this code doesn't allow for home angle, max angle, current angle
  10. // the loop in this code only dynamically accounts for array length, not height
  11. //
  12. // x x-position to spawn ship center at
  13. // y y-position to spawn ship center at
  14. // blueprint blueprint to spawn
  15. // ss what sprite should be assigned to obj_ship
  16. //
  17.  
  18. var xp, yp, bp, ss, n, m;
  19.  
  20. xp = argument0; // x position to spawn at
  21. yp = argument1; // y position to spawn at
  22. bp = argument2; // blueprint to spawn
  23. ss = argument3; // ship sprite
  24.  
  25. n = 0;
  26. m = 0;
  27.  
  28. // inst will change for each iteration through blueprint
  29. var inst = noone;
  30.  
  31. // ship is the first thing that will be spawned at n = 0
  32. owner = noone;
  33.  
  34. // iterate through ship blueprint
  35. for (n = 0; n < array_height_2d(bp); n++) {
  36.  
  37.  
  38. inst = instance_create( xp + bp[n, BP_X_OFFSET], yp + bp[n, BP_Y_OFFSET], bp[n, BP_OBJECT] );
  39.  
  40. // this is not a temp val as with statement must call it
  41. side = bp[n, BP_PLACEMENT];
  42.  
  43. // get z axis location for component
  44. inst.z = bp[n, BP_Z];
  45. inst.depth = (-1) * inst.z;
  46.  
  47. // owner not set yet
  48. if (owner == noone) {
  49.  
  50. // assign obj_ship as owner for other components
  51. owner = inst;
  52.  
  53. // set ship sprite to provided sprite
  54. inst.sprite_index = ss;
  55.  
  56. // set x and y location for spawn
  57. xp = owner.x;
  58. yp = owner.y;
  59. }
  60.  
  61. // with the spawned object
  62. with (inst) {
  63.  
  64. // assign component owner
  65. owner = other.owner;
  66.  
  67. // if buildable is a gun then add to the ship gun array
  68. // all ship components have a key set in their create event
  69. if (string_pos("gun", key)) {
  70. with(owner) {
  71.  
  72. // inform gun about what number it is
  73. other.gun_number = gun_count;
  74.  
  75. // add this gun to ships list of gun ids
  76. gun_array[gun_count] = other.id;
  77.  
  78. // replace this with array_length at some point
  79. gun_count += 1;
  80.  
  81. // assign gun it's slot location
  82. side = other.side;
  83. }
  84.  
  85. // this will init. home / current / max angles, requires side to be set
  86. scr_weapon_angle_setup(side);
  87.  
  88. // if buildable is an engine
  89. } else if (string_pos("engine", key)) {
  90. with(owner) {
  91.  
  92. // inform engine about what number it is
  93. other.engine_number = engine_count;
  94.  
  95. // add this engine to ships list of engine ids
  96. engine_array[engine_count] = other.id;
  97.  
  98. // replace this with array_length at some point
  99. engine_count += 1;
  100. }
  101. }
  102.  
  103. // set instances offset from center of ship
  104. X_offset = x - owner.x;
  105. Y_offset = y - owner.y;
  106.  
  107. // create physics fixture if object is a component not base ship
  108. // consider changing this to just check for n > 1
  109. if !(string_pos("obj_ship", key)) scr_add_fixture(1, owner);
  110.  
  111. }
  112.  
  113. show_debug_message(object_get_name(inst.object_index) + " created with owner " + object_get_name(inst.owner.object_index) + " " + string(inst.owner));
  114.  
  115. }
  116.  
  117. return owner;
Add Comment
Please, Sign In to add comment