• No results found

Dealing with dates and time

In document Designing Games with Game Maker (Page 147-153)

In Game Maker there are a number of functions to deal with dates and time. A date-time combination is stored in a real number. The integral part of a date-time value is the number of days that have passed since 12/30/1899. The fractional part of the date-time value is fraction of a 24 hour day that has elapsed. The following functions exist:

date_current_datetime() Returns the date-time value that corresponds to the current moment.

date_current_date() Returns the date-time value that corresponds to the current date only (ignoring the time).

date_current_time() Returns the date-time value that corresponds to the current time only (ignoring the date).

date_create_datetime(year,month,day,hour,minute,second) Creates a date-time value corresponding to the indicated date and time.

date_create_date(year,month,day) Creates a date-time value corresponding to the indicated date.

date_create_time(hour,minute,second) Creates a date-time value corresponding to the indicated time.

date_valid_datetime(year,month,day,hour,minute,second) Returns whether the indicated date and time are valid.

date_valid_date(year,month,day) Returns whether the indicated date is valid.

date_valid_time(hour,minute,second) Returns whether the indicated time is valid.

date_inc_year(date,amount) Returns a new date that is amount years after the indicated date. amount must be an integer number.

date_inc_month(date,amount) Returns a new date that is amount months after the indicated date. amount must be an integer number.

date_inc_week(date,amount) Returns a new date that is amount weeks after the indicated date. amount must be an integer number.

date_inc_day(date,amount) Returns a new date that is amount days after the indicated date. amount must be an integer number.

date_inc_hour(date,amount) Returns a new date that is amount hours after the indicated date. amount must be an integer number.

date_inc_minute(date,amount) Returns a new date that is amount minutes after the indicated date. amount must be an integer number.

date_inc_second(date,amount) Returns a new date that is amount seconds after the indicated date. amount must be an integer number.

date_get_year(date) Returns the year corresponding to the date.

date_get_month(date) Returns the month corresponding to the date.

date_get_week(date) Returns the week of the year corresponding to the date.

date_get_day(date) Returns the day of the month corresponding to the date.

date_get_hour(date) Returns the hour corresponding to the date.

date_get_minute(date) Returns the minute corresponding to the date.

date_get_second(date) Returns the second corresponding to the date.

date_get_weekday(date) Returns the day of the week corresponding to the date.

date_get_day_of_year(date) Returns the day of the year corresponding to the date.

date_get_hour_of_year(date) Returns the hour of the year corresponding to the date.

date_get_minute_of_year(date) Returns the minute of the year corresponding to the date.

date_get_second_of_year(date) Returns the second of the year corresponding to the date.

date_year_span(date1,date2) Returns the number of years between the two dates. It reports incomplete years as a fraction.

date_month_span(date1,date2) Returns the number of months between the two dates. It reports incomplete months as a fraction.

date_week_span(date1,date2) Returns the number of weeks between the two dates. It reports incomplete weeks as a fraction.

date_day_span(date1,date2) Returns the number of days between the two dates. It reports incomplete days as a fraction.

date_hour_span(date1,date2) Returns the number of hours between the two dates. It reports incomplete hours as a fraction.

date_minute_span(date1,date2) Returns the number of minutes between the two dates. It reports incomplete minutes as a fraction.

date_second_span(date1,date2) Returns the number of seconds between the two dates. It reports incomplete seconds as a fraction.

date_compare_datetime(date1,date2) Compares the two date-time values. Returns -1, 0, or 1 depending on whether the first is smaller, equal, or larger than the second value.

taking the date part into account. Returns -1, 0, or 1 depending on whether the first is smaller, equal, or larger than the second value.

date_compare_time(date1,date2) Compares the two date-time values only taking the time part into account. Returns -1, 0, or 1 depending on whether the first is smaller, equal, or larger than the second value.

date_date_of(date) Returns the date part of the indicated date-time value, setting the time part to 0.

date_time_of(date) Returns the time part of the indicated date-time value, setting the date part to 0.

date_datetime_string(date) Returns a string indicating the given date and time in the default format for the system.

date_date_string(date) Returns a string indicating the given date in the default format for the system.

date_time_string(date) Returns a string indicating the given time in the default format for the system.

date_days_in_month(date) Returns the number of days in the month indicated by the date-time value.

date_days_in_year(date) Returns the number of days in the year indicated by the date-time value.

date_leap_year(date) Returns whether the year indicated by the date-time value is a leap year.

date_is_today(date) Returns whether the indicated date-time value is on today.

Game play

There are a large number of variables and functions that you can use to define the game play. These in particular influence the movement and creation of instances, the timing, the room, and the handling of events.

Information on game play can be found in the following pages: Moving Around

Paths

Motion Planning Collision Detection Instances

Deactivating Instances Timing

Rooms Score

Generating Events

Miscellaneous Variables and Functions

Moving around

Obviously, an important aspect of games is the moving around of object instances. Each instance has two built-in variables x and y that indicate the position of the instance. (To be precise, they indicate the place where the origin of the sprite is placed. Position (0,0) is the top-left corner of the room. You can change the position of the instance by changing its x and y variables. If you want the object to make complicated motions this is the way to go. You typically put this code in the step event for the object.

If the object moves with constant speed and direction, there is an easier way to do this. Each object instance has a horizontal speed (hspeed) and a vertical speed (vspeed). Both are indicated in pixels per step. A positive horizontal speed means a motion to the right, a negative horizontal speed mean a motion to the left. Positive vertical speed is downwards and negative vertical speed is upwards. So you have to set these variables only once (for example in the creating event) to give the object instance a constant motion.

There is quite a different way for specifying motion, using a direction (in degrees 0-359), and a speed (should be non-negative). You can set and read these variables to specify an arbitrary motion.

(Internally this is changed into values for hspeed and vspeed.) Also there is the friction and the gravity and gravity direction. Finally, there is the function motion_add(dir,speed) to add a motion to the current one.

To be complete, each instance has the following variables and functions dealing with its position and motion:

x Its x-position.

y Its y-position.

xprevious Its previous x-position.

yprevious Its previous y-position.

ystart Its starting y-position in the room.

hspeed Horizontal component of the speed.

vspeed Vertical component of the speed.

direction Its current direction (0-360, counter-clockwise, 0 = to the right).

speed Its current speed (pixels per step).

friction Current friction (pixels per step).

gravity Current amount of gravity (pixels per step).

gravity_direction Direction of gravity (270 is downwards).

motion_set(dir,speed) Sets the motion with the given speed in direction dir.

motion_add(dir,speed) Adds the motion to the current motion (as a vector addition).

There are a large number of functions available that help you in defining your motions:

place_free(x,y) Returns whether the instance placed at position(x,y) is collision- free. This is typically used as a check before actually moving to the new position.

place_empty(x,y) Returns whether the instance placed at position (x,y) meets nobody. So this function takes also non-solid instances into account.

place_meeting(x,y,obj) Returns whether the instance placed at position (x,y) meets obj. obj can be an object in which case the function returns true is some instance of that object is met. It can also be an instance id, the special word all meaning an instance of any object, or the special word other.

place_snapped(hsnap,vsnap) Returns whether the instance is aligned with the snapping values.

move_random(hsnap,vsnap) Moves the instance to a free random, snapped position, like the corresponding action.

move_snap(hsnap,vsnap) Snaps the instance, like the corresponding action.

move_wrap(hor,vert,margin) Wraps the instance when it has left the room to the other side. hor indicates whether to wrap horizontaly and vert indicates whether to wrap vertically. margin indicates how far the origin of the instance must be outside the room before the wrap happens. So it is a margin around the room. You typically use this function in the Outside event.

move_towards_point(x,y,sp) Moves the instances with speed sp toward position (x,y).

move_bounce_solid(adv) Bounces against solid instances, like the corresponding action. adv indicates whether to use advance bounce, that also takes slanted walls into

account.

move_bounce_all(adv) Bounces against all instances, instead of just the solid ones.

move_contact_solid(dir,maxdist) Moves the instance in the direction until a contact position with a solid object is reached. If there is no collision at the current position, the instance is placed just before a collision occurs. If there already is a collision the instance is not moved. You can specify the maximal distance to move (use a negative number for an arbitrary distance).

move_contact_all(dir,maxdist) Same as the previous function but this time you stop at a contact with any object, not just solid objects.

move_outside_solid(dir,maxdist) Moves the instance in the direction until it no longer lies within a solid object. If there is no collision at the current position the instance is not moved. You can specify the maximal distance to move (use a negative number for an arbitrary distance).

move_outside_all(dir,maxdist) Same as the previous function but this time you move until outside any object, not just solid objects.

distance_to_point(x,y) Returns the distance of the bounding box of the current instance to (x,y). (If the instance does not have a sprite or mask, the result of the function is undefined.)

distance_to_object(obj) Returns the distance of the instance to the nearest instance of object obj. (If the instance or object does not have a sprite or mask, the result of the function is undefined.)

position_empty(x,y) Returns whether there is nothing at position (x,y).

position_meeting(x,y,obj) Returns whether at position (x,y) there is an instance obj. obj can be an object, an instance id, or the keywords self, other, or all.

Paths

In Game Maker you can define paths and order instances to follow such paths. Although you can use actions for this, there are functions and variables that give you more flexibility:

path_start(path,speed,endaction,absolute) Starts a path for the current instance. The path is the name of the path you want to start. The speed is the speed with which the path must be followed. A negative speed means that the instance

moves backwards along the path. The endaction indicates what should happen when the end of the path is reached. The following values can be used:

0 : stop the path

1: continue from the start position (if the path is not closed we jump to the start position

2: continue from the current position

3: reverse the path, that is change the sign of the speed

The argument absolute should be true or false. When true the absolute coordinates of the path are used. When false the path is relative to the current position of the instance. To be more precise, if the speed is positive, the start point of the path will be placed on the current position and the path is followed from there. When the speed is negative the end point of the path will be placed on the current position and the path is followed backwards from there.

path_end() Ends the following of a path for the current instance.

path_index* Index of the current path the instance follows. You cannot change this directly but must use the function above.

path_position Position in the current path. 0 is the beginning of the path. 1 is the end of the path. The value must lie between 0 and 1.

path_positionprevious Previous position in the current path. This can be used e.g. in collision events to set the position on the path back to the previous position.

path_speed Speed (in pixels per step) with which the path must be followed. Use a negative speed to move backwards.

path_orientation Orientation (counter-clockwise) into which the path is performed. 0 is the normal orientation of the path.

path_scale Scale of the path. Increase to make the path larger. 1 is the default value.

path_endaction The action that must be performed at the end of the path. You can use the values indicated above.

In document Designing Games with Game Maker (Page 147-153)