Skip to content

Instantly share code, notes, and snippets.

@multimentha
Created August 2, 2019 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save multimentha/c3b51086a38473e7f7ef8d460454653c to your computer and use it in GitHub Desktop.
Save multimentha/c3b51086a38473e7f7ef8d460454653c to your computer and use it in GitHub Desktop.
GameMaker Studio - FriendlyCosmonaut's Farming RPG Tutorial: require player to to be close to the soil
///@description instance_create_crop
///@arg x
///@arg y
///@arg crop_type
// snap x,y position to a grid
var cs = crops.cell_size;
var gx = argument0 div cs;
var gy = argument1 div cs;
var i_grid = crops.ds_crop_instances;
var cell = i_grid[# gx, gy];
// only plant crop if the current grid cell is empty
if(cell == 0){
xx = gx*cs;
yy = gy*cs;
// check for soil
// (could be wrapped into a custom script)
var lay_id = layer_get_id("T_soil");
var map_id = layer_tilemap_get_id(lay_id);
var data = tilemap_get_at_pixel(map_id, argument0, argument1);
if (data==0){
show_debug_message("there is no soil here");
return false
} else {
show_debug_message("there IS soil here");
}
// player needs to be close to the planting spot, so that you can't plant from across the room
// max distance is 1 grid cell away (diagonal connection counts as 1 cell away too)
max_dist_h = 1; // horizontal
max_dist_v = 1; // vertical
// find player's location on tile grid
player_grid_x = obj_player.x div cs;
player_grid_y = obj_player.y div cs;
// calculate distance between player and planting spot (where the mouse pointer is)
dist_h = abs(player_grid_x - gx); // horizontal
dist_v = abs(player_grid_y - gy); // vertical
// disallow planting if not close enough
var close_enough = dist_h <= max_dist_h and dist_v <= max_dist_v;
if (!close_enough){
show_debug_message("You're not close enough to plant that!")
return false;
}
// create the instance
var inst = instance_create_layer(xx+cs/2, yy+cs/2, "Instances", obj_crop);
i_grid[# gx, gy] = inst;
// define characteristics
with(inst){
crop_type = argument2;
growth_stage_duration = crops.ds_crop_types[# 0, crop_type]; // 0 means first (seed) stage
}
// return the instance ID of the freshly created crop to the caller
return inst;
} else {
show_debug_message("There is already something there.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment