Skip to content

Commit

Permalink
v2.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sohomsahaun committed Oct 11, 2021
1 parent 669bb9b commit 497f3c8
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1 align="center">SnowState 2.3.0</h1>
<h1 align="center">SnowState 2.4.0</h1>
<p align="center">@sohomsahaun</p>

---
Expand Down
3 changes: 2 additions & 1 deletion SnowState.yyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion objects/oGame/Draw_64.gml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ if (showHistory) {
_y = 10;

var _str, _states, _i;
_states = oPlayer.fsm.get_history();
_states = oPlayer.fsm.history_get();
_str = "groundAttack3";

draw_set_alpha(.9);
Expand Down
2 changes: 1 addition & 1 deletion objects/oGame/Step_0.gml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
showControls ^= keyboard_check_released(vk_f2);
showHistory ^= keyboard_check_released(ord("H"));
showHistory ^= keyboard_check_released(ord("H"));
2 changes: 1 addition & 1 deletion objects/oPlayer/Create_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fsm = new SnowState("idle");

fsm
.history_enable()
.set_history_max_size(20)
.history_set_max_size(20)
.event_set_default_function("draw", function() {
// Draw this no matter what state we are in
// (Unless it is overridden, ofcourse)
Expand Down
13 changes: 13 additions & 0 deletions options/operagx/options_operagx.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 60 additions & 29 deletions scripts/SnowState/SnowState.gml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* SnowState | v2.3.0
* SnowState | v2.4.0
* Documentation: https://github.com/sohomsahaun/SnowState/wiki
*
* Author: Sohom Sahaun | @sohomsahaun
Expand Down Expand Up @@ -31,6 +31,7 @@ function SnowState(_initState) constructor {
initState = _initState;
execEnter = _execEnter;
currEvent = undefined;
tempEvent = undefined;
parent = {};
childQueue = [];
history = array_create(2, undefined);
Expand All @@ -48,25 +49,10 @@ function SnowState(_initState) constructor {
};

add = method(other, function(_name, _struct, _hasParent) {
if (_struct == undefined) _struct = {};
if (_hasParent == undefined) _hasParent = false;
var _events, _state, _event, _i;
var _self = self;

with (__this) {
if (!is_string(_name) || (_name == "")) {
snowstate_error("State name should be a non-empty string.");
return undefined;
}

if (SNOWSTATE_DEBUG_WARNING && is_state_defined(_name)) {
snowstate_trace("State \"", _name, "\" has been defined already. The previous definition has been replaced.");
}

if (!is_struct(_struct)) {
snowstate_error("State struct should be a struct.");
return undefined;
}

_state = create_events_struct(_struct);
states[$ _name] = _state;

Expand All @@ -91,7 +77,10 @@ function SnowState(_initState) constructor {

// Execute "enter" event
if (_name == initState) {
if (execEnter) other.enter();
if (execEnter) {
stateStartTime = get_timer();
_self.enter();
}
}
}
});
Expand Down Expand Up @@ -128,23 +117,35 @@ function SnowState(_initState) constructor {
});

change = method(other, function(_state, _leave, _enter) {
var _defLeave, _defEnter;
var _defLeave, _defEnter, _self;
_self = self;
_defLeave = leave;
_defEnter = enter;
leave = _leave;
enter = _enter;

leave();
with (__this) {
// Leave current state
tempEvent = _defLeave;
with (_self) leave();

// Add to history
if (array_length(childQueue) > 0) {
history[@ 0] = childQueue[@ 0];
childQueue = [];
}

// Init state
stateStartTime = get_timer();
history_add(_state);

// Enter next state
tempEvent = _defEnter;
with (_self) enter();

// Reset temp variable
tempEvent = undefined;
}
enter();

leave = _defLeave;
enter = _defEnter;
Expand Down Expand Up @@ -339,6 +340,7 @@ function SnowState(_initState) constructor {
#region Basics

add = function(_name, _struct) {
if (_struct == undefined) _struct = {};
__this.add(_name, _struct, false);
return self;
};
Expand Down Expand Up @@ -410,15 +412,35 @@ function SnowState(_initState) constructor {

get_time = function(_seconds) {
if (_seconds == undefined) _seconds = false;
var _time = (get_timer()-__this.stateStartTime) * 1/1000000;
return (_seconds ? _time : floor(_time * game_get_speed(gamespeed_fps)));

with (__this) {
var _time = (get_timer()-stateStartTime) * 1/1000000;
return (_seconds ? _time : (_time * game_get_speed(gamespeed_fps)));
}
};

set_time = function(_time, _seconds) {
if (_seconds == undefined) _seconds = false;

with (__this) {
if (!is_real(_time)) {
snowstate_error("Time should be a number");
return undefined;
}
if (!_seconds) _time *= 1/game_get_speed(gamespeed_fps);
stateStartTime = get_timer() - _time;
}

return self;
};

#endregion

#region Inheritance

add_child = function(_parent, _name, _struct) {
if (_struct == undefined) _struct = {};

with (__this) {
if (!is_string(_parent) || (_parent == "")) {
snowstate_error("State name should be a non-empty string.");
Expand Down Expand Up @@ -518,6 +540,12 @@ function SnowState(_initState) constructor {
return self;
};

event_get_current_function = function() {
with (__this) {
return tempEvent;
}
}

event_exists = function(_event) {
try {
return __this.states[$ get_current_state()][$ _event].exists;
Expand Down Expand Up @@ -564,7 +592,7 @@ function SnowState(_initState) constructor {
return __this.historyEnabled;
};

set_history_max_size = function(_size) {
history_set_max_size = function(_size) {
with (__this) {
if (!is_real(_size)) {
snowstate_error("Size should be a number.");
Expand All @@ -582,17 +610,19 @@ function SnowState(_initState) constructor {

return self;
};
set_history_max_size = history_set_max_size;

get_history_max_size = function() {
history_get_max_size = function() {
return __this.historyMaxSize;
};
get_history_max_size = history_get_max_size;

get_history = function() {
history_get = function() {
var _prev = get_previous_state();
with (__this) {
if (!historyEnabled) {
if (SNOWSTATE_DEBUG_WARNING) {
snowstate_trace("History is disabled, can not get_history().");
snowstate_trace("History is disabled, can not history_get().");
}
return [];
}
Expand All @@ -604,6 +634,7 @@ function SnowState(_initState) constructor {
return _arr;
}
};
get_history = history_get;

#endregion

Expand All @@ -618,7 +649,7 @@ function SnowState(_initState) constructor {

}

#macro SNOWSTATE_VERSION "v2.3.0"
#macro SNOWSTATE_DATE "30-06-2021"
#macro SNOWSTATE_VERSION "v2.4.0"
#macro SNOWSTATE_DATE "11-10-2021"

show_debug_message("[SnowState] You are using SnowState by @sohomsahaun (Version: " + string(SNOWSTATE_VERSION) + " | Date: " + string(SNOWSTATE_DATE) + ")");

0 comments on commit 497f3c8

Please sign in to comment.