Giter Club home page Giter Club logo

Comments (8)

fineconstant avatar fineconstant commented on June 18, 2024 1

I used VecDaque in my implementation:

#[derive(Default)]
pub struct KeyDownQueue {
    pub keys_pressed: VecDeque<KeyCode>,
}

from rust-sokoban.

fineconstant avatar fineconstant commented on June 18, 2024 1

@iolivia , @denniskaselow
Take a look at #91 - what do you think? :)

from rust-sokoban.

iolivia avatar iolivia commented on June 18, 2024

good catch, we probably want a queue not a stack!

from rust-sokoban.

denniskaselow avatar denniskaselow commented on June 18, 2024

Something simple as this could also be sufficient:

impl InputQueue {
    pub fn get_first(&mut self) -> Option<KeyCode> {
        if self.keys_pressed.is_empty() {
            None
        } else {
            Some(self.keys_pressed.remove(0))
        }
    }
}

But I'm new to Rust, so I have no idea if this is the Rust way to do things, but it's what I'd do, considering the key_pressed Vec will never have more than a few keys even in worst case (ignoring automated input).

from rust-sokoban.

iolivia avatar iolivia commented on June 18, 2024

that looks great @fineconstant! feel free to submit a PR, will be very helpful for others using the repo 😄

from rust-sokoban.

fineconstant avatar fineconstant commented on June 18, 2024

I looked into it and I think that Vec<KeyCode> is sufficient, so there is no need to make any changes.

With Vec<KeyCode> calling input_queue.keys_pressed.pop() function removes the last element indeed, but input_queue.keys_pressed.push(keycode); appends an element at the back of the collection - it should not cause any problems.

std::vec::Vec - Rust #method.push
std::vec::Vec - Rust #method.pop

I've just recently started learning Rust so I may be wrong, but if you agree then I think we can close this issue 😄

from rust-sokoban.

denniskaselow avatar denniskaselow commented on June 18, 2024

Like I said in the initial comment:

If you input keys fast enough (very easy when not using release mode and batch rendering) this can be seen because of erratic movement of the player character.

A push can happen several times in the time it takes one pop to move the character. Thus you can input up, left, down and it'll move the character down, left, up. So your VecDeque would be much better.

from rust-sokoban.

fineconstant avatar fineconstant commented on June 18, 2024

Oh I see now, you are right. I will prepare a PR with changes 😃

from rust-sokoban.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.