ユーザ用ツール

サイト用ツール


youtube:monogame-ball

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
youtube:monogame-ball [2024/02/28 18:34] – ↷ 移動操作に合わせてリンクを書き換えました。 freemikanyoutube:monogame-ball [2024/02/28 18:36] (現在) freemikan
行 1: 行 1:
-====== 倉庫番 ======+====== 画面を跳ね回るボール ======
  
-{{youtube:sokoban.png?400|}}+{{:youtube:monogame-myballgame.png|}}
  
-src/main.rs+===== Game1.cs =====
  
-<file rust+<file csharp
-use bevy::prelude::*+using Microsoft.Xna.Framework
-use bevy::time::FixedTimestep;+using Microsoft.Xna.Framework.Graphics; 
 +using Microsoft.Xna.Framework.Input;
  
-const ARENA_WIDTH: i32 = 10; +namespace MyBallGame;
-const ARENA_HEIGHT: i32 = 10; +
-const TILE_SIZE: Vec2 = Vec2::new(30.0, 30.0); +
-const SCREEN_SIZE: Vec2 = Vec2::new( +
-    TILE_SIZE.x * ARENA_WIDTH as f32, +
-    TILE_SIZE.y * ARENA_HEIGHT as f32, +
-); +
-const PLAYER_COLOR: Color = Color::AQUAMARINE; +
-const CARGO_COLOR: Color = Color::BEIGE; +
-const GOAL_COLOR: Color = Color::CRIMSON;+
  
-fn main() { +public class Game1 Game 
-    App::new() +
-        .add_plugins(DefaultPlugins.set(WindowPlugin +    private GraphicsDeviceManager _graphics; 
-            window: WindowDescriptor { +    private SpriteBatch _spriteBatch; 
-                title: "SOKOBAN".to_string(), +    private Ball _ball;
-                width: SCREEN_SIZE.x, +
-                height: SCREEN_SIZE.y, +
-                ..default() +
-            }, +
-            ..default() +
-        })) +
-        .insert_resource(ClearColor(Color::BLUE)) +
-        .add_startup_system(setup) +
-        .add_system(bevy::window::close_on_esc) +
-        .add_system_set( +
-            SystemSet::new() +
-                .with_run_criteria(FixedTimestep::step(1.0 / 60.0)) // BUG: any other FPS of 60 may not handle keyboard input as expected +
-                .with_system(move_player_and_cargo) +
-                .with_system(position_translation.after(move_player_and_cargo)) +
-                .with_system(check_level_complete.after(move_player_and_cargo)) +
-                .with_system(complete_level.after(check_level_complete)), +
-        ) +
-        .add_event::<LevelCompleteEvent>() +
-        .run(); +
-}+
  
-#[derive(Component, Debug, PartialEq, Eq, Clone, Copy)] +    const int GAME_WIDTH = 320; 
-struct Position { +    const int GAME_HEIGHT = 480;
-    x: i32, +
-    y: i32, +
-    z: i32, +
-}+
  
-#[derive(Component)] +    public Game1() 
-struct Cargo;+    { 
 +        _graphics = new GraphicsDeviceManager(this); 
 +        Content.RootDirectory = "Content"; 
 +        IsMouseVisible = true;
  
-#[derive(Component)+        _graphics.PreferredBackBufferWidth = GAME_WIDTH; 
-struct Player;+        _graphics.PreferredBackBufferHeight = GAME_HEIGHT; 
 +        _graphics.ApplyChanges(); 
 +    }
  
-#[derive(Component)] +    protected override void Initialize() 
-struct Goal;+    { 
 +        // TODO: Add your initialization logic here
  
-#[derive(Default)+        base.Initialize(); 
-struct LevelCompleteEvent;+    }
  
-fn setup(mut commands: Commands) { +    protected override void LoadContent() 
-    commands.spawn(Camera2dBundle::default());+    
 +        _spriteBatch = new SpriteBatch(GraphicsDevice);
  
-    // spawn Player +        var ballTexture = Content.Load<Texture2D>("Circle"); 
-    commands +        var ballRadius = ballTexture.Width / 2.0f; 
-        .spawn(SpriteBundle { +        var ballPosition = new Vector2(GAME_WIDTH / 2.0f - ballRadiusGAME_HEIGHT / 2.0f - ballRadius); 
-            sprite: Sprite { +        var ballVelocity = new Vector2(300.0f300.0f); 
-                color: PLAYER_COLOR, +        _ball = new Ball(ballPositionballRadiusballVelocityballTexture); 
-                ..default() +    }
-            }, +
-            transform: Transform { +
-                scale: Vec3::new(TILE_SIZE.xTILE_SIZE.y, 0.0), +
-                ..default() +
-            }, +
-            ..default(+
-        }) +
-        .insert(Player) +
-        .insert(Position { +
-            x: ARENA_WIDTH / 2, +
-            y: 0, +
-            z: 3, +
-        });+
  
-    // spawn Cargo +    protected override void Update(GameTime gameTime)
-    for (x, y) in (0..ARENA_WIDTH) +
-        .step_by(2) +
-        .zip(std::iter::repeat(ARENA_HEIGHT - 3)) +
-        .chain( +
-            (1..ARENA_WIDTH) +
-                .step_by(2) +
-                .zip(std::iter::repeat(ARENA_HEIGHT - 4)), +
-        )+
     {     {
-        commands +        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) 
-            .spawn(SpriteBundle { +            Exit();
-                sprite: Sprite { +
-                    color: CARGO_COLOR, +
-                    ..default() +
-                }, +
-                transform: Transform { +
-                    scale: Vec3::new(TILE_SIZE.x * 0.8, TILE_SIZE.y * 0.8, 0.0)+
-                    ..default(+
-                }, +
-                ..default() +
-            }+
-            .insert(Cargo) +
-            .insert(Position { x, y, z: 2 }); +
-    }+
  
-    // spawn Goal +        _ball.Update(gameTimeGAME_WIDTHGAME_HEIGHT);
-    for x in 0..ARENA_WIDTH { +
-        commands +
-            .spawn(SpriteBundle { +
-                sprite: Sprite { +
-                    color: GOAL_COLOR, +
-                    ..default() +
-                }, +
-                transform: Transform { +
-                    scale: Vec3::new(TILE_SIZE.x * 0.9, TILE_SIZE.y * 0.9, 0.0), +
-                    ..default() +
-                }, +
-                ..default() +
-            }) +
-            .insert(Goal) +
-            .insert(Position { +
-                x, +
-                y: ARENA_HEIGHT - 1, +
-                z: 1, +
-            }); +
-    } +
-}+
  
-fn position_translation(mut query: Query<(&Position, &mut Transform)>) { +        base.Update(gameTime);
-    for (p, mut t) in query.iter_mut() { +
-        t.translation = Vec3::new( +
-            p.x as f32 * TILE_SIZE.x - (SCREEN_SIZE.x / 2.0) + (TILE_SIZE.x / 2.0), +
-            p.y as f32 * TILE_SIZE.y - (SCREEN_SIZE.y / 2.0) + (TILE_SIZE.y / 2.0), +
-            p.z as f32, +
-        );+
     }     }
-} 
  
-#[derive(Debug, Copy, Clone, PartialEq, Eq)] +    protected override void Draw(GameTime gameTime
-enum Direction +    
-    Up, +        GraphicsDevice.Clear(Color.CornflowerBlue);
-    Down, +
-    Left, +
-    Right, +
-    None, +
-}+
  
-fn move_player_and_cargo( +        _spriteBatch.Begin(); 
-    keyboard_input: Res<Input<KeyCode>>, +        _ball.Draw(_spriteBatch); 
-    mut player_positions: Query<&mut Position, (With<Player>, Without<Cargo>)>, +        _spriteBatch.End();
-    mut cargo_positions: Query<&mut Position, (With<Cargo>, Without<Player>)>, +
-) { +
-    let direction = if keyboard_input.just_pressed(KeyCode::Up) { +
-        println!("up"); +
-        Direction::Up +
-    } else if keyboard_input.just_pressed(KeyCode::Down{ +
-        Direction::Down +
-    } else if keyboard_input.just_pressed(KeyCode::Left+
-        Direction::Left +
-    } else if keyboard_input.just_pressed(KeyCode::Right) { +
-        Direction::Right +
-    } else { +
-        Direction::None +
-    };+
  
-    let mut player_position = player_positions.single_mut();+        base.Draw(gameTime); 
 +    } 
 +}
  
-    let next_position = |p: &Position, direction, step| match direction { +class Ball 
-        Direction::Up => Position +
-            x: p.x, +    private Vector2 _position; 
-            y: p.y + step, +    private float _radius; 
-            z: p.z, +    private Vector2 _velocity; 
-        }, +    private Texture2D _texture;
-        Direction::Down => Position { +
-            x: p.x, +
-            y: p.y - step, +
-            z: p.z, +
-        }, +
-        Direction::Left => Position { +
-            x: p.x - step, +
-            y: p.y, +
-            z: p.z, +
-        }, +
-        Direction::Right => Position { +
-            x: p.x + step, +
-            y: p.y, +
-            z: p.z, +
-        }, +
-        _ => *p, +
-    };+
  
-    let inside_arene = +    public Ball(Vector2 position, float radiusVector2 velocityTexture2D texture)
-        |p: &Position| p.x >= 0 && p.y >= 0 && p.x < ARENA_WIDTH && p.y < ARENA_HEIGHT; +
- +
-    // try to push cargo and update player and cargo position if it possible +
-    let new_player_position = next_position(&player_positiondirection1); +
-    let new_cargo_position = next_position(&player_positiondirection, 2); +
-    let new_cargo_position_empty = cargo_positions +
-        .iter() +
-        .all(|cp| !(cp.x == new_cargo_position.x && cp.y == new_cargo_position.y)); +
- +
-    if let Some(mut cargo_forward) = cargo_positions +
-        .iter_mut() +
-        .find(|cp| cp.x == new_player_position.x && cp.y == new_player_position.y)+
     {     {
-        if new_cargo_position_empty && inside_arene(&new_cargo_position) { +        _position position
-            *cargo_forward new_cargo_position+        _radius radius
-            *player_position new_player_position+        _velocity = velocity; 
-        +        _texture texture;
-    } else { +
-        if inside_arene(&new_player_position) { +
-            *player_position new_player_position; +
-        }+
     }     }
-} 
  
-fn check_level_complete( +    public void Update(GameTime gameTimeint gameWidthint gameHeight
-    debug_input: Res<Input<KeyCode>>, +    
-    cargo_positions: Query<&Position(With<Cargo>, Without<Goal>)>, +        float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; 
-    goal_positions: Query<&Position, (With<Goal>, Without<Cargo>)>, +        _position = _position + _velocity * deltaTime; 
-    mut level_complete_writer: EventWriter<LevelCompleteEvent>, + 
-) { +        if (_position.X <= 0.0f || _position.X + _radius * 2.0f >= gameWidth
-    if debug_input.just_pressed(KeyCode::D{ +        
-        level_complete_writer.send_default()+            _velocity.X *= -1.0f
-        return; +        }
-    }+
  
-    for cargo in cargo_positions.iter() { +        if (_position.Y <0.0f || _position.Y + _radius * 2.0f >gameHeight
-        let maybe_on_goal goal_positions +        { 
-            .iter() +            _velocity.Y *= -1.0f;
-            .find(|gpgp.x == cargo.x && gp.== cargo.y); +
-        if maybe_on_goal.is_none() +
-            return;+
         }         }
     }     }
-    level_complete_writer.send_default(); 
-} 
  
-fn complete_level( +    public void Draw(SpriteBatch spriteBatch
-    mut commands: Commands, +    { 
-    events: EventReader<LevelCompleteEvent>, +        spriteBatch.Draw(_texture_position, Color.Green);
-    asset_server: Res<AssetServer>, +
-{ +
-    if !events.is_empty() +
-        events.clear(); +
- +
-        println!("Complete"); +
- +
-        let font_size = 50.0; +
- +
-        // show screen message +
-        commands.spawn( +
-            TextBundle::from_section( +
-                "COMPLETE!", +
-                TextStyle { +
-                    font: asset_server.load("fonts/RubikGemstones-Regular.ttf"), +
-                    font_size, +
-                    color: Color::PINK, +
-                }, +
-            ) +
-            .with_text_alignment(TextAlignment::CENTER) // what does this effect? +
-            .with_style(Style { +
-                position_type: PositionType::Absolute, +
-                position: UiRect { +
-                    // hard-code position to display text center of screen +
-                    top: Val::Px(SCREEN_SIZE.y / 2.0 - 60.0 / 2.0), +
-                    left: Val::Px(SCREEN_SIZE.x / 2.0 - font_size * 2.5), +
-                    ..default() +
-                }, +
-                ..default() +
-            }), +
-        );+
     }     }
 } }
 </file> </file>
  
-Cargo.toml+===== Program.cs =====
  
-<file> 
-[package] 
-name = "sokoban" 
-version = "0.1.0" 
-edition = "2021" 
  
-# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html+<file csharp> 
 +using var game = new MyBallGame.Game1(); 
 +game.Run(); 
 +</file>
  
-[dependencies] 
-bevy = { version = "0.9.1", features = [ "dynamic" ] } 
- 
-[profile.dev] 
-opt-level = 1 
- 
-[profile.dev.package."*"] 
-opt-level = 3 
-</file> 
  
youtube/monogame-ball.1709112853.txt.gz · 最終更新: 2024/02/28 18:34 by freemikan

特に明示されていない限り、本Wikiの内容は次のライセンスに従います: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki