add enemy

This commit is contained in:
artem 2024-01-28 22:20:04 +03:00
parent 628dd5ad8a
commit 2e462de3cd
34 changed files with 156 additions and 103 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

52
game/src/enemy.rs Normal file
View File

@ -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 => {}
}
}
}

View File

@ -1,113 +1,25 @@
//! Game project.
use fyrox::{
engine::GraphicsContext,
keyboard::PhysicalKey,
asset::manager::ResourceManager,
core::{pool::Handle, log::Log, impl_component_provider},
event::{ElementState, Event, WindowEvent, TouchPhase},
keyboard::KeyCode,
core::pool::Handle,
event::Event,
gui::message::UiMessage,
plugin::{Plugin, PluginConstructor, PluginContext, PluginRegistrationContext},
scene::{
Scene,
graph::Graph,
},
script::{ScriptContext, ScriptTrait}
};
use std::path::Path;
mod map;
mod player;
mod enemy;
use map::build_map;
use player::Player;
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 {
fn register(&self, context: PluginRegistrationContext) {
// Register your scripts here.

View File

@ -3,16 +3,20 @@ use std::collections::VecDeque;
use fyrox::{
core::{
algebra::{Vector2, Vector3},
impl_component_provider,
log::Log,
pool::Handle,
reflect::prelude::*,
uuid_provider,
visitor::prelude::*,
},
keyboard::KeyCode,
event::{ElementState, Event, WindowEvent, TouchPhase},
engine::GraphicsContext,
keyboard::{KeyCode, PhysicalKey},
scene::animation::spritesheet::SpriteSheetAnimation,
scene::dim2::{rectangle::Rectangle, rigidbody::RigidBody},
scene::node::Node,
script::ScriptContext,
script::{ScriptContext, ScriptTrait}
};
#[derive(Visit, Reflect, Debug, Clone, Default)]
@ -27,7 +31,6 @@ pub struct Player {
#[reflect(hidden)]
#[visit(skip)]
current_animation: VecDeque<u32>,
current_frame: u32,
start_y_position: f32,
start_fight_last: f32,
@ -42,7 +45,7 @@ enum Animations {
Steps = 0,
Idle = 1,
Jump = 2,
Fight = 3
Fight = 3,
}
uuid_provider!(Player = "c5671d19-9f1a-4286-8486-add4ebaadaec");
@ -106,7 +109,7 @@ impl Player {
// self.fight = true;
// return;
// }
},
}
_ => (),
}
}
@ -201,7 +204,7 @@ impl Player {
match self.current_animation.front() {
Some(data) => {
last_anim = Some(data.clone());
},
}
None => {}
}
match last_anim {
@ -214,9 +217,7 @@ impl Player {
}
self.current_animation.push_front(animation as u32)
}
None => {
self.current_animation.push_front(animation as u32)
}
None => self.current_animation.push_front(animation as u32),
}
}
fn animate_choose(&mut self, context: &mut ScriptContext) {
@ -246,11 +247,10 @@ impl Player {
}
}
pub fn animation_do(&mut self, context: &mut ScriptContext) {
self.animate_choose(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() {
match self.current_animation.pop_back() {
Some(anim) => {
@ -259,7 +259,7 @@ impl Player {
None => {}
}
}
let current_animation = self.animations.get_mut(current_animation );
let current_animation = self.animations.get_mut(current_animation);
match current_animation {
Some(animation_data) => {
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);
}
}