add enemy
|
After Width: | Height: | Size: 17 KiB |
|
After Width: | Height: | Size: 23 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 24 KiB |
|
After Width: | Height: | Size: 20 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 27 KiB |
|
After Width: | Height: | Size: 26 KiB |
|
After Width: | Height: | Size: 24 KiB |
|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 23 KiB |
|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 20 KiB |
|
After Width: | Height: | Size: 20 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 24 KiB |
|
After Width: | Height: | Size: 23 KiB |
|
After Width: | Height: | Size: 25 KiB |
|
After Width: | Height: | Size: 26 KiB |
|
After Width: | Height: | Size: 23 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 20 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 19 KiB |
|
After Width: | Height: | Size: 26 KiB |
|
After Width: | Height: | Size: 26 KiB |
|
After Width: | Height: | Size: 24 KiB |
|
|
@ -0,0 +1,52 @@
|
||||||
|
use fyrox::{
|
||||||
|
core::{
|
||||||
|
impl_component_provider, pool::Handle, reflect::prelude::*, uuid_provider,
|
||||||
|
visitor::prelude::*,
|
||||||
|
},
|
||||||
|
scene::animation::spritesheet::SpriteSheetAnimation,
|
||||||
|
scene::dim2::collider::Collider,
|
||||||
|
scene::node::Node,
|
||||||
|
script::{ScriptContext, ScriptTrait},
|
||||||
|
};
|
||||||
|
|
||||||
|
impl_component_provider!(Enemy,);
|
||||||
|
uuid_provider!(Enemy = "a5671d19-9f1a-4286-8486-add4ebaadaec");
|
||||||
|
|
||||||
|
#[derive(Visit, Reflect, Debug, Clone, Default)]
|
||||||
|
pub struct Enemy {
|
||||||
|
sprite: Handle<Node>,
|
||||||
|
move_left: bool,
|
||||||
|
move_right: bool,
|
||||||
|
jump: bool,
|
||||||
|
animations: Vec<SpriteSheetAnimation>,
|
||||||
|
fight: bool,
|
||||||
|
current_animation: u32,
|
||||||
|
|
||||||
|
handle: Handle<Node>,
|
||||||
|
player_handle: Handle<Node>,
|
||||||
|
player_collider: Handle<Node>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ScriptTrait for Enemy {
|
||||||
|
fn on_init(&mut self, context: &mut ScriptContext) {
|
||||||
|
// Store reference to *this* instance of the enemy node
|
||||||
|
self.handle = context.handle;
|
||||||
|
|
||||||
|
// Find the Player node
|
||||||
|
match context.scene.graph.find_by_name_from_root("Player") {
|
||||||
|
// (Handle<Node>, Node)
|
||||||
|
Some(handle) => {
|
||||||
|
// If found, store the handle
|
||||||
|
self.player_handle = handle.0;
|
||||||
|
|
||||||
|
// Find and store the Player's collider node handle
|
||||||
|
for child in handle.1.children().iter() {
|
||||||
|
if let Some(_) = context.scene.graph[*child].cast::<Collider>() {
|
||||||
|
self.player_collider = *child;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,113 +1,25 @@
|
||||||
//! Game project.
|
//! Game project.
|
||||||
use fyrox::{
|
use fyrox::{
|
||||||
engine::GraphicsContext,
|
|
||||||
keyboard::PhysicalKey,
|
|
||||||
asset::manager::ResourceManager,
|
asset::manager::ResourceManager,
|
||||||
core::{pool::Handle, log::Log, impl_component_provider},
|
core::pool::Handle,
|
||||||
event::{ElementState, Event, WindowEvent, TouchPhase},
|
event::Event,
|
||||||
keyboard::KeyCode,
|
|
||||||
gui::message::UiMessage,
|
gui::message::UiMessage,
|
||||||
plugin::{Plugin, PluginConstructor, PluginContext, PluginRegistrationContext},
|
plugin::{Plugin, PluginConstructor, PluginContext, PluginRegistrationContext},
|
||||||
scene::{
|
scene::{
|
||||||
Scene,
|
Scene,
|
||||||
graph::Graph,
|
graph::Graph,
|
||||||
},
|
},
|
||||||
script::{ScriptContext, ScriptTrait}
|
|
||||||
};
|
};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
mod map;
|
mod map;
|
||||||
mod player;
|
mod player;
|
||||||
|
mod enemy;
|
||||||
|
|
||||||
use map::build_map;
|
use map::build_map;
|
||||||
use player::Player;
|
use player::Player;
|
||||||
|
|
||||||
pub struct GameConstructor;
|
pub struct GameConstructor;
|
||||||
|
|
||||||
impl_component_provider!(Player,);
|
|
||||||
|
|
||||||
impl ScriptTrait for Player {
|
|
||||||
// Called once at initialization.
|
|
||||||
fn on_init(&mut self, context: &mut ScriptContext) {
|
|
||||||
self.init(context);
|
|
||||||
self.change_orientation(context);
|
|
||||||
self.window_height = 600;
|
|
||||||
self.window_height = 800;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put start logic - it is called when every other script is already initialized.
|
|
||||||
fn on_start(&mut self, _context: &mut ScriptContext) {}
|
|
||||||
|
|
||||||
// Called whenever there is an event from OS (mouse click, keypress, etc.)
|
|
||||||
fn on_os_event(&mut self, event: &Event<()>, context: &mut ScriptContext) {
|
|
||||||
if let Event::Resumed{} = event{
|
|
||||||
if let GraphicsContext::Initialized(ref graphics_context) = context.graphics_context {
|
|
||||||
let window_size = graphics_context.window.inner_size();
|
|
||||||
self.window_height = window_size.height;
|
|
||||||
self.window_width = window_size.width;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let Event::WindowEvent { event, .. } = event {
|
|
||||||
if let WindowEvent::KeyboardInput { event, .. } = event {
|
|
||||||
if let PhysicalKey::Code(keycode) = event.physical_key {
|
|
||||||
let is_pressed = event.state == ElementState::Pressed;
|
|
||||||
self.key_checker(context, keycode, is_pressed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let WindowEvent::Touch(touch) = event {
|
|
||||||
match touch.phase {
|
|
||||||
TouchPhase::Started => {
|
|
||||||
Log::info(format!("touch x {:?}, y {:?}", touch.location.x, touch.location.y));
|
|
||||||
if touch.location.x > (self.window_width / 2) as f64 {
|
|
||||||
self.key_checker(context, KeyCode::ArrowRight, true);
|
|
||||||
self.key_checker(context, KeyCode::ArrowLeft, false);
|
|
||||||
} else {
|
|
||||||
self.key_checker(context, KeyCode::ArrowLeft, true);
|
|
||||||
self.key_checker(context, KeyCode::ArrowRight, false);
|
|
||||||
}
|
|
||||||
if touch.location.y < (self.window_height / 2) as f64 {
|
|
||||||
self.key_checker(context, KeyCode::ArrowUp, true);
|
|
||||||
}
|
|
||||||
if touch.location.y > (self.window_height / 2) as f64 {
|
|
||||||
self.key_checker(context, KeyCode::ArrowUp, false);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
TouchPhase::Ended => {
|
|
||||||
if touch.location.x > (self.window_width / 2) as f64 {
|
|
||||||
self.key_checker(context, KeyCode::ArrowRight, false);
|
|
||||||
} else {
|
|
||||||
self.key_checker(context, KeyCode::ArrowLeft, false);
|
|
||||||
}
|
|
||||||
if touch.location.y < (self.window_height / 2) as f64 {
|
|
||||||
self.key_checker(context, KeyCode::ArrowUp, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
},
|
|
||||||
_ => {},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let WindowEvent::Resized(size) = event {
|
|
||||||
Log::info(format!("Resized xheight{:?}, width {:?}", size.height, size.width));
|
|
||||||
self.window_height = size.height;
|
|
||||||
self.window_width = size.width;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Called every frame at fixed rate of 60 FPS.
|
|
||||||
fn on_update(&mut self, context: &mut ScriptContext) {
|
|
||||||
// The script can be assigned to any scene node, but we assert that it will work only with
|
|
||||||
// 2d rigid body nodes.
|
|
||||||
self.jump_do(context);
|
|
||||||
self.move_do(context);
|
|
||||||
self.change_orientation(context);
|
|
||||||
self.animation_do(context);
|
|
||||||
|
|
||||||
self.loop_over(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PluginConstructor for GameConstructor {
|
impl PluginConstructor for GameConstructor {
|
||||||
fn register(&self, context: PluginRegistrationContext) {
|
fn register(&self, context: PluginRegistrationContext) {
|
||||||
// Register your scripts here.
|
// Register your scripts here.
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,20 @@ use std::collections::VecDeque;
|
||||||
use fyrox::{
|
use fyrox::{
|
||||||
core::{
|
core::{
|
||||||
algebra::{Vector2, Vector3},
|
algebra::{Vector2, Vector3},
|
||||||
|
impl_component_provider,
|
||||||
|
log::Log,
|
||||||
pool::Handle,
|
pool::Handle,
|
||||||
reflect::prelude::*,
|
reflect::prelude::*,
|
||||||
uuid_provider,
|
uuid_provider,
|
||||||
visitor::prelude::*,
|
visitor::prelude::*,
|
||||||
},
|
},
|
||||||
keyboard::KeyCode,
|
event::{ElementState, Event, WindowEvent, TouchPhase},
|
||||||
|
engine::GraphicsContext,
|
||||||
|
keyboard::{KeyCode, PhysicalKey},
|
||||||
scene::animation::spritesheet::SpriteSheetAnimation,
|
scene::animation::spritesheet::SpriteSheetAnimation,
|
||||||
scene::dim2::{rectangle::Rectangle, rigidbody::RigidBody},
|
scene::dim2::{rectangle::Rectangle, rigidbody::RigidBody},
|
||||||
scene::node::Node,
|
scene::node::Node,
|
||||||
script::ScriptContext,
|
script::{ScriptContext, ScriptTrait}
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Visit, Reflect, Debug, Clone, Default)]
|
#[derive(Visit, Reflect, Debug, Clone, Default)]
|
||||||
|
|
@ -27,7 +31,6 @@ pub struct Player {
|
||||||
#[reflect(hidden)]
|
#[reflect(hidden)]
|
||||||
#[visit(skip)]
|
#[visit(skip)]
|
||||||
current_animation: VecDeque<u32>,
|
current_animation: VecDeque<u32>,
|
||||||
current_frame: u32,
|
|
||||||
|
|
||||||
start_y_position: f32,
|
start_y_position: f32,
|
||||||
start_fight_last: f32,
|
start_fight_last: f32,
|
||||||
|
|
@ -42,7 +45,7 @@ enum Animations {
|
||||||
Steps = 0,
|
Steps = 0,
|
||||||
Idle = 1,
|
Idle = 1,
|
||||||
Jump = 2,
|
Jump = 2,
|
||||||
Fight = 3
|
Fight = 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
uuid_provider!(Player = "c5671d19-9f1a-4286-8486-add4ebaadaec");
|
uuid_provider!(Player = "c5671d19-9f1a-4286-8486-add4ebaadaec");
|
||||||
|
|
@ -106,7 +109,7 @@ impl Player {
|
||||||
// self.fight = true;
|
// self.fight = true;
|
||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
},
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -201,7 +204,7 @@ impl Player {
|
||||||
match self.current_animation.front() {
|
match self.current_animation.front() {
|
||||||
Some(data) => {
|
Some(data) => {
|
||||||
last_anim = Some(data.clone());
|
last_anim = Some(data.clone());
|
||||||
},
|
}
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
match last_anim {
|
match last_anim {
|
||||||
|
|
@ -214,9 +217,7 @@ impl Player {
|
||||||
}
|
}
|
||||||
self.current_animation.push_front(animation as u32)
|
self.current_animation.push_front(animation as u32)
|
||||||
}
|
}
|
||||||
None => {
|
None => self.current_animation.push_front(animation as u32),
|
||||||
self.current_animation.push_front(animation as u32)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn animate_choose(&mut self, context: &mut ScriptContext) {
|
fn animate_choose(&mut self, context: &mut ScriptContext) {
|
||||||
|
|
@ -246,11 +247,10 @@ impl Player {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn animation_do(&mut self, context: &mut ScriptContext) {
|
pub fn animation_do(&mut self, context: &mut ScriptContext) {
|
||||||
self.animate_choose(context);
|
self.animate_choose(context);
|
||||||
//let can_update_frame = self.is_need_update_anim_frame(context);
|
//let can_update_frame = self.is_need_update_anim_frame(context);
|
||||||
let mut current_animation = Animations::Idle as usize;
|
let mut current_animation = Animations::Idle as usize;
|
||||||
if !self.current_animation.is_empty() {
|
if !self.current_animation.is_empty() {
|
||||||
match self.current_animation.pop_back() {
|
match self.current_animation.pop_back() {
|
||||||
Some(anim) => {
|
Some(anim) => {
|
||||||
|
|
@ -259,7 +259,7 @@ impl Player {
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let current_animation = self.animations.get_mut(current_animation );
|
let current_animation = self.animations.get_mut(current_animation);
|
||||||
match current_animation {
|
match current_animation {
|
||||||
Some(animation_data) => {
|
Some(animation_data) => {
|
||||||
animation_data.update(context.dt);
|
animation_data.update(context.dt);
|
||||||
|
|
@ -282,3 +282,92 @@ impl Player {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl_component_provider!(Player,);
|
||||||
|
|
||||||
|
impl ScriptTrait for Player {
|
||||||
|
// Called once at initialization.
|
||||||
|
fn on_init(&mut self, context: &mut ScriptContext) {
|
||||||
|
self.init(context);
|
||||||
|
self.change_orientation(context);
|
||||||
|
self.window_height = 600;
|
||||||
|
self.window_height = 800;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put start logic - it is called when every other script is already initialized.
|
||||||
|
fn on_start(&mut self, _context: &mut ScriptContext) {}
|
||||||
|
|
||||||
|
// Called whenever there is an event from OS (mouse click, keypress, etc.)
|
||||||
|
fn on_os_event(&mut self, event: &Event<()>, context: &mut ScriptContext) {
|
||||||
|
if let Event::Resumed {} = event {
|
||||||
|
if let GraphicsContext::Initialized(ref graphics_context) = context.graphics_context {
|
||||||
|
let window_size = graphics_context.window.inner_size();
|
||||||
|
self.window_height = window_size.height;
|
||||||
|
self.window_width = window_size.width;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
|
if let WindowEvent::KeyboardInput { event, .. } = event {
|
||||||
|
if let PhysicalKey::Code(keycode) = event.physical_key {
|
||||||
|
let is_pressed = event.state == ElementState::Pressed;
|
||||||
|
self.key_checker(context, keycode, is_pressed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let WindowEvent::Touch(touch) = event {
|
||||||
|
match touch.phase {
|
||||||
|
TouchPhase::Started => {
|
||||||
|
Log::info(format!(
|
||||||
|
"touch x {:?}, y {:?}",
|
||||||
|
touch.location.x, touch.location.y
|
||||||
|
));
|
||||||
|
if touch.location.x > (self.window_width / 2) as f64 {
|
||||||
|
self.key_checker(context, KeyCode::ArrowRight, true);
|
||||||
|
self.key_checker(context, KeyCode::ArrowLeft, false);
|
||||||
|
} else {
|
||||||
|
self.key_checker(context, KeyCode::ArrowLeft, true);
|
||||||
|
self.key_checker(context, KeyCode::ArrowRight, false);
|
||||||
|
}
|
||||||
|
if touch.location.y < (self.window_height / 2) as f64 {
|
||||||
|
self.key_checker(context, KeyCode::ArrowUp, true);
|
||||||
|
}
|
||||||
|
if touch.location.y > (self.window_height / 2) as f64 {
|
||||||
|
self.key_checker(context, KeyCode::ArrowUp, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TouchPhase::Ended => {
|
||||||
|
if touch.location.x > (self.window_width / 2) as f64 {
|
||||||
|
self.key_checker(context, KeyCode::ArrowRight, false);
|
||||||
|
} else {
|
||||||
|
self.key_checker(context, KeyCode::ArrowLeft, false);
|
||||||
|
}
|
||||||
|
if touch.location.y < (self.window_height / 2) as f64 {
|
||||||
|
self.key_checker(context, KeyCode::ArrowUp, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let WindowEvent::Resized(size) = event {
|
||||||
|
Log::info(format!(
|
||||||
|
"Resized xheight{:?}, width {:?}",
|
||||||
|
size.height, size.width
|
||||||
|
));
|
||||||
|
self.window_height = size.height;
|
||||||
|
self.window_width = size.width;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Called every frame at fixed rate of 60 FPS.
|
||||||
|
fn on_update(&mut self, context: &mut ScriptContext) {
|
||||||
|
// The script can be assigned to any scene node, but we assert that it will work only with
|
||||||
|
// 2d rigid body nodes.
|
||||||
|
self.jump_do(context);
|
||||||
|
self.move_do(context);
|
||||||
|
self.change_orientation(context);
|
||||||
|
self.animation_do(context);
|
||||||
|
|
||||||
|
self.loop_over(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||