<commit_before>#include "stdafx.h"
#include "animation_system.h"
#include "entity_system/world.h"
#include "../messages/animate_message.h"

using namespace messages;
using namespace resources;

void animation_system::consume_events(world& owner) {
	auto events = owner.get_message_queue<animate_message>();

	for (auto it : events) {
		auto ptr = it.subject->find<components::animate>();
		if (!ptr) continue; auto& animate = *ptr;

		if (it.animation_priority >= animate.current_priority || animate.current_state == components::animate::state::PAUSED) {
			if (it.change_speed)
				animate.speed_factor = it.speed_factor;

			if (it.change_animation) {
				resources::animation* new_instance = nullptr;
				if (it.set_animation)
					new_instance = it.set_animation;
				else {
					auto found_animation = animate.available_animations->get_raw().find(it.animation_type);
					if (found_animation != animate.available_animations->get_raw().end()) {
						new_instance = (*found_animation).second;
					}
				}

				if (new_instance != animate.current_animation) {
					animate.current_animation = new_instance;
					if (!it.preserve_state_if_animation_changes) {
						animate.set_current_frame(0, it.subject);
						animate.current_ms = 0.f;
					}
					else {
						/* update callback */
						animate.set_current_frame(animate.get_current_frame(), it.subject);
					}
				}
			}
			
			animate.current_priority = it.animation_priority;

			switch (it.message_type) {
			case animate_message::type::PAUSE:
				if (animate.current_state != components::animate::state::PAUSED) {
					animate.paused_state = animate.current_state;
					animate.current_state = components::animate::state::PAUSED;
				}
				break;
			case animate_message::type::STOP:
				animate.paused_state = components::animate::state::INCREASING;
				animate.current_state = components::animate::state::PAUSED;
				animate.set_current_frame(0, it.subject);
				animate.current_ms = 0.f;
				break;
			case animate_message::type::START:
				animate.current_state = components::animate::state::INCREASING;
				animate.set_current_frame(0, it.subject);
				animate.current_ms = 0.f;
				break;
			case animate_message::type::CONTINUE:
				if (animate.current_state == components::animate::state::PAUSED) {
					animate.current_state = animate.paused_state;
				}
				break;
			default: break;
			}
		}
	}
}

void call(luabind::object func, augs::entity_system::entity* subject) {
	if (func) {
		try {
			luabind::call_function<void>(func, subject);
		}
		catch (std::exception compilation_error) {
			std::cout << compilation_error.what() << '\n';
		}
	}
}

void components::animate::set_current_frame(unsigned number, augs::entity_system::entity* subject) {
	if (saved_callback_out) {
		call(saved_callback_out, subject);
	}
	current_frame = number;
	if (current_animation) {
		call(current_animation->frames[current_frame].callback, subject);
		saved_callback_out = current_animation->frames[current_frame].callback_out;
	} else
	saved_callback_out = luabind::object();
}

void components::animate::set_current_animation_set(resources::animate_info* set, augs::entity_system::entity* subject) {
	if (saved_callback_out) {
		call(saved_callback_out, subject);
	}
	available_animations = set;
	current_frame = 0;<commit_msg>setting frame on set_current_frame<commit_after>#include "stdafx.h"
#include "animation_system.h"
#include "entity_system/world.h"
#include "../messages/animate_message.h"

using namespace messages;
using namespace resources;

void animation_system::consume_events(world& owner) {
	auto events = owner.get_message_queue<animate_message>();

	for (auto it : events) {
		auto ptr = it.subject->find<components::animate>();
		if (!ptr) continue; auto& animate = *ptr;

		if (it.animation_priority >= animate.current_priority || animate.current_state == components::animate::state::PAUSED) {
			if (it.change_speed)
				animate.speed_factor = it.speed_factor;

			//if (it.change_animation) {
				resources::animation* new_instance = nullptr;
				if (it.set_animation)
					new_instance = it.set_animation;
				else {
					auto found_animation = animate.available_animations->get_raw().find(it.animation_type);
					if (found_animation != animate.available_animations->get_raw().end()) {
						new_instance = (*found_animation).second;
					}
				}

				if (new_instance != animate.current_animation) {
					animate.current_animation = new_instance;

					if (it.message_type == animate_message::type::CONTINUE) {
						it.message_type = animate_message::type::START;
					}
					//if (!it.preserve_state_if_animation_changes) {
					//	animate.set_current_frame(0, it.subject);
					//	animate.current_ms = 0.f;
					//}
					//else {
						/* update callback */
						//animate.set_current_frame(animate.get_current_frame(), it.subject);
					//}
				}
			//}
			
			animate.current_priority = it.animation_priority;

			switch (it.message_type) {
			case animate_message::type::PAUSE:
				if (animate.current_state != components::animate::state::PAUSED) {
					animate.paused_state = animate.current_state;
					animate.current_state = components::animate::state::PAUSED;
				}
				break;
			case animate_message::type::STOP:
				animate.paused_state = components::animate::state::INCREASING;
				animate.current_state = components::animate::state::PAUSED;
				animate.set_current_frame(0, it.subject);
				animate.current_ms = 0.f;
				break;
			case animate_message::type::START:
				animate.current_state = components::animate::state::INCREASING;
				animate.set_current_frame(0, it.subject);
				animate.current_ms = 0.f;
				break;
			case animate_message::type::CONTINUE:
				if (animate.current_state == components::animate::state::PAUSED) {
					animate.current_state = animate.paused_state;
				}
				break;
			default: break;
			}
		}
	}
}

void call(luabind::object func, augs::entity_system::entity* subject) {
	if (func) {
		try {
			luabind::call_function<void>(func, subject);
		}
		catch (std::exception compilation_error) {
			std::cout << compilation_error.what() << '\n';
		}
	}
}

void components::animate::set_current_frame(unsigned number, augs::entity_system::entity* subject) {
	if (saved_callback_out) {
		call(saved_callback_out, subject);
	}
	current_frame = number;
	if (current_animation) {
		call(current_animation->frames[current_frame].callback, subject);
		saved_callback_out = current_animation->frames[current_frame].callback_out;

		subject->get<components::render>().model = &current_animation->frames[get_current_frame()].model;
	} else
	saved_callback_out = luabind::object();
}

void components::animate::set_current_animation_set(resources::animate_info* set, augs::entity_system::entity* subject) {
	if (saved_callback_out) {
		call(saved_callback_out, subject);
	}
	available_animations = set;
	current_frame = 0;
{'fraction_non_alphanumeric': 0.13308984660336012, 'fraction_numerical': 0.0020452885317750183, 'mean_word_length': 4.584013050570962, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2390911', 'n_tokens_mistral': 2646, 'n_tokens_neox': 2154, 'n_words': 347}