To implement the described resident behavior in your game, we'll expand upon your existing GameMaker Language (GML) scripts. The goal is to create a dynamic environment where residents patrol during day and night, respond to vampire sightings, hide appropriately, and ensure smooth navigation without getting stuck. Additionally, we'll handle the spawning of new residents weekly.
Here's a comprehensive approach to achieve this:
1. Game Controller Enhancements
The obj_game_controller manages the day/night cycle and triggers corresponding behaviors. We'll enhance it to handle weekly events for spawning new residents.
Create Event:
/// @description obj_game_controller Create
day_length = 600; // 5 minutes in frames
night_length = 600; // 5 minutes in frames
week_length = 42000; // Assuming 60 FPS: 42000 frames ≈ 700 seconds ≈ 11.67 minutes
global.time_counter = 0;
global.is_day = true;
global.week_counter = 0;
Step Event:
/// @description obj_game_controller Step
global.time_counter += 1;
global.week_counter += 1;
// Toggle day/night
if (global.is_day && global.time_counter >= day_length) {
global.is_day = false;
global.time_counter = 0;
// Trigger night behaviors
event_perform(ev_other, ev_user0); // You can define specific events or functions
}
if (!global.is_day && global.time_counter >= night_length) {
global.is_day = true;
global.time_counter = 0;
// Trigger day behaviors
event_perform(ev_other, ev_user1); // You can define specific events or functions
}
// Spawn new residents weekly
if (global.week_counter >= week_length) {
global.week_counter = 0;
instance_create_layer(random(room_width), random(room_height), "Instances", obj_resident_m);
// You can adjust the number of residents spawned per week as needed
}
2. Resident Object Enhancements (obj_resident_m)
We'll structure the resident behavior based on day/night cycles, handling patrols, hiding, and responses to vampire sightings.
Variables and Initialization
In the Create Event, initialize necessary variables:
/// @description obj_resident_m Create
state = global.is_day ? "day_patrol" : "night_patrol";
spd = 4; // Adjust speed as necessary
target_x = x;
target_y = y;
hiding = false;
vampire_near = false;
current_path = undefined;
// Set initial patrol points
patrol_points = ds_list_create();
for (var i = 0; i < 5; i++) { // Example: 5 patrol points
ds_list_add(patrol_points, [random(room_width), random(room_height)]);
}
current_patrol_index = 0;
// Reference to house and church objects (assuming single instances)
house = instance_nearest(x, y, obj_house);
church = instance_nearest(x, y, obj_church);
Clean Up
In the Cleanup Event, ensure to destroy any data structures:
/// @description obj_resident_m Cleanup
ds_list_destroy(patrol_points);
Step Event:
/// @description obj_resident_m Step
// Sight detection for vampires
var seen_vampire = instance_nearest(x, y, obj_vampire);
if (seen_vampire != noone) {
var distance_to_vampire = point_distance(x, y, seen_vampire.x, seen_vampire.y);
if (distance_to_vampire < 100) { // Sight range
vampire_near = true;
} else {
vampire_near = false;
}
} else {
vampire_near = false;
}
if (vampire_near) {
// Run away behavior
state = "fleeing";
flee_from(seen_vampire.x, seen_vampire.y);
} else {
// Continue with normal behavior based on day/night
if (global.is_day) {
state = "day_patrol";
} else {
state = "night_patrol";
}
}
// Execute behavior based on state
switch (state) {
case "day_patrol":
patrol_behavior();
break;
case "night_patrol":
patrol_behavior();
break;
case "fleeing":
// Already handled in flee_from()
break;
}
// Avoid obstacles and pathfinding
path_follow();
Behavioral Functions
1. Patrol Behavior:
/// @description obj_resident_m patrol_behavior
if (distance_to_point(target_x, target_y) < 4) {
// Choose next patrol point
current_patrol_index = (current_patrol_index + 1) mod ds_list_size(patrol_points);
var next_point = ds_list_find_value(patrol_points, current_patrol_index);
target_x = next_point[0];
target_y = next_point[1];
}
move_towards_point(target_x, target_y, spd);
2. Fleeing Behavior:
/// @description obj_resident_m flee_from
function flee_from(vampire_x, vampire_y) {
// Determine flee direction (away from vampire)
var dir = point_direction(vampire_x, vampire_y, x, y);
var flee_distance = 200; // Distance to run away
target_x = x + lengthdir_x(flee_distance, dir);
target_y = y + lengthdir_y(flee_distance, dir);
// Ensure target is within room boundaries
target_x = clamp(target_x, 0, room_width);
target_y = clamp(target_y, 0, room_height);
// Optionally, move towards nearest house or church for safety
var closest_safe_spot;
var distance_to_house = point_distance(x, y, house.x, house.y);
var distance_to_church = point_distance(x, y, church.x, church.y);
if (distance_to_house < distance_to_church) {
closest_safe_spot = [house.x, house.y];
} else {
closest_safe_spot = [church.x, church.y];
}
// Move towards the safe spot
path_start(destination = closest_safe_spot, spd, path_action_stop, false);
}
3. Path Following and Obstacle Avoidance:
To ensure residents navigate smoothly without getting stuck, implement pathfinding using GameMaker's built-in path functions or a custom grid-based system. Here's a simplified approach using move_towards_point with collision checks:
/// @description obj_resident_m path_follow
if (collision_point(x + lengthdir_x(spd, direction), y + lengthdir_y(spd, direction), obj_obstacle, true, true)) {
// Choose a new random direction to avoid obstacle
direction += choose(-90, 90);
} else {
move_towards_point(target_x, target_y, spd);
}
For more robust pathfinding, consider using GameMaker's pathfinding functions:
/// @description obj_resident_m path_follow_with_pathfinding
if (!path_exists(current_path)) {
// Create a path to the target
current_path = path_add();
path_start(current_path, speed, path_action_stop, false);
path_move_to(current_path, target_x, target_y);
} else {
// Continue following the path
path_continue(current_path);
}
Ensure you handle path removal when the target changes or when the resident reaches the destination.
3. Handling New Resident Spawning
As included in the obj_game_controller Step event, new residents are spawned every week_length frames. You can adjust week_length based on your game's frame rate and desired real-time duration.
/// Spawning a new resident
instance_create_layer(random(room_width), random(room_height), "Instances", obj_resident_m);
If you wish to spawn multiple residents per week, wrap the spawning line in a loop:
var residents_to_spawn = 2; // Number of residents to spawn each week
for (var i = 0; i < residents_to_spawn; i++) {
instance_create_layer(random(room_width), random(room_height), "Instances", obj_resident_m);
}
4. Hiding in Houses
Residents can hide in houses to avoid vampires. Modify the fleeing behavior to prioritize hiding locations.
/// @description obj_resident_m flee_from (modified)
function flee_from(vampire_x, vampire_y) {
// Determine flee direction (away from vampire)
var dir = point_direction(vampire_x, vampire_y, x, y);
var flee_distance = 200; // Distance to run away
var tentative_x = x + lengthdir_x(flee_distance, dir);
var tentative_y = y + lengthdir_y(flee_distance, dir);
// Ensure within room boundaries
tentative_x = clamp(tentative_x, 0, room_width);
tentative_y = clamp(tentative_y, 0, room_height);
// Check if within range to a house or church
var distance_to_house = point_distance(tentative_x, tentative_y, house.x, house.y);
var distance_to_church = point_distance(tentative_x, tentative_y, church.x, church.y);
if (distance_to_house < distance_to_church) {
target_x = house.x;
target_y = house.y;
} else {
target_x = church.x;
target_y = church.y;
}
}
Ensure that obj_house and obj_church have defined collision boxes or areas where residents can hide.
5. Ensuring Residents Don’t Get Stuck
To prevent residents from getting stuck on obstacles or navigating poorly:
-
Use Transparent Collision Objects: Make sure obstacles are properly defined with collision masks.
-
Implement Smooth Pathfinding: Utilize GameMaker's pathfinding with proper path recalculations when encountering dynamic obstacles.
-
Limit Direction Changes: Prevent rapid direction changes that might cause residents to oscillate between states.
-
Boundary Checks: Ensure that target points are always within navigable areas.
6. Additional Enhancements
-
Resident Inside/Outside Logic:
If some residents stay inside while others patrol:
/// @description obj_resident_m Create (modified)
staying_inside = (random(1) > 0.5); // 50% chance to stay inside
if (staying_inside) {
state = "inside";
} else {
state = global.is_day ? "day_patrol" : "night_patrol";
}
Modify the Step event accordingly to handle the "inside" state, where residents remain in or return to their house.
-
Dynamic Obstacle Handling:
For dynamic environments, consider updating patrol points or recalculating paths when obstacles are added or removed.
-
Smoothing Movements:
Implement movement smoothing to make transitions between states appear natural.
7. Example Complete Step Event for obj_resident_m
Combining all the above, here's a more complete Step event example:
/// @description obj_resident_m Step
// Update state based on day/night and whether staying inside
if (staying_inside) {
state = "inside";
} else if (vampire_near) {
state = "fleeing";
} else {
state = global.is_day ? "day_patrol" : "night_patrol";
}
// Handle behaviors based on state
switch (state) {
case "day_patrol":
case "night_patrol":
patrol_behavior();
break;
case "fleeing":
flee_from(seen_vampire.x, seen_vampire.y);
break;
case "inside":
// Stay at house position
x = house.x;
y = house.y;
break;
}
// Sight detection for vampires
var seen_vampire = instance_nearest(x, y, obj_vampire);
if (seen_vampire != noone) {
var distance_to_vampire = point_distance(x, y, seen_vampire.x, seen_vampire.y);
vampire_near = (distance_to_vampire < 100); // Sight range
} else {
vampire_near = false;
}
// Path following and obstacle avoidance
path_follow();
8. Final Notes
-
Optimization: Ensure that the number of pathfinding calculations is optimized, especially if you have many residents. Consider limiting path recalculations or using simpler movement logic where possible.
-
Testing: Rigorously test resident behaviors in various scenarios to fine-tune parameters like speed, sight range, and patrol point distribution.
-
Scalability: For larger numbers of residents, consider using data structures like grids or spatial partitioning to manage residents efficiently.
By implementing these enhancements, your residents will exhibit complex behaviors that make your game environment more immersive and responsive to in-game events like vampire appearances.