Skip to content

Instantly share code, notes, and snippets.

@drakeirving
Last active April 8, 2016 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drakeirving/1ef0f9a315468dc864a678caf550116b to your computer and use it in GitHub Desktop.
Save drakeirving/1ef0f9a315468dc864a678caf550116b to your computer and use it in GitHub Desktop.
// For use by player scripts
// Focus movement locks options in position relative to the player
task gradius_options(head, num_options, wait){
let options = [ head ];
loop(num_options){
options = options ~ [ follow_option(options[length(options)-1], wait) ];
}
function follow_option(parent, wait){
let option = ObjEnemy_Create(OBJ_ENEMY);
ObjEnemy_Regist(option);
ObjPrim_SetTexture(option, dir~"bullet1.png");
ObjSprite2D_SetSourceRect(option, 32, 32, 48, 48);
ObjSprite2D_SetDestCenter(option);
ObjMove_SetPosition(option, ObjMove_GetX(parent), ObjMove_GetY(parent));
let hist = array(wait, [0, 0]);
let index = 0;
task follow(option, parent, wait){
let is_moving;
let last_pos = [ObjMove_GetX(parent), ObjMove_GetY(parent)];
while(!Obj_IsDeleted(parent)){
is_moving = (ObjMove_GetX(parent) != last_pos[0]) || (ObjMove_GetY(parent) != last_pos[1]);
if(is_moving){
if(GetVirtualKeyState(VK_SLOWMOVE) % 2 == 0){
// unfocused movement
ObjMove_SetPosition(option,
ObjMove_GetX(option) + hist[index][0],
ObjMove_GetY(option) + hist[index][1]);
hist[index] = [ObjMove_GetX(parent) - last_pos[0],
ObjMove_GetY(parent) - last_pos[1]];
index = (index + 1) % wait;
}else{
// focused movement
ObjMove_SetPosition(option,
ObjMove_GetX(option) + (ObjMove_GetX(parent) - last_pos[0]),
ObjMove_GetY(option) + (ObjMove_GetY(parent) - last_pos[1]));
}
}
last_pos = [ObjMove_GetX(parent), ObjMove_GetY(parent)];
yield;
}
}
follow(option, parent, wait);
return option;
}
}
// More general version
// Attach to any Move object as head
task gradius_options(head, num_options, wait){
let options = [ head ];
loop(num_options){
options = options ~ [ follow_option(options[length(options)-1], wait) ];
}
function follow_option(parent, wait){
let option = ObjEnemy_Create(OBJ_ENEMY);
ObjEnemy_Regist(option);
ObjPrim_SetTexture(option, dir~"bullet1.png");
ObjSprite2D_SetSourceRect(option, 32, 32, 48, 48);
ObjSprite2D_SetDestCenter(option);
ObjMove_SetPosition(option, ObjMove_GetX(parent), ObjMove_GetY(parent));
let hist = array(wait, [0, 0]);
let index = 0;
task follow(option, parent, wait){
let is_moving;
let last_pos = [ObjMove_GetX(parent), ObjMove_GetY(parent)];
while(!Obj_IsDeleted(parent)){
is_moving = (ObjMove_GetX(parent) != last_pos[0]) || (ObjMove_GetY(parent) != last_pos[1]);
if(is_moving){
ObjMove_SetPosition(option,
ObjMove_GetX(option) + hist[index][0],
ObjMove_GetY(option) + hist[index][1]);
hist[index] = [ObjMove_GetX(parent) - last_pos[0],
ObjMove_GetY(parent) - last_pos[1]];
index = (index + 1) % wait;
}
last_pos = [ObjMove_GetX(parent), ObjMove_GetY(parent)];
yield;
}
Obj_Delete(option);
}
follow(option, parent, wait);
return option;
}
}
// Array init utility
function array(size, value){
let fill = [value];
while(length(fill) < size * 2){
fill = fill ~ fill;
}
return fill[0..size];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment