15

const bool Game::isColliding(... This indicates that the returned type is const, and should not be modified by any part of your code. But it's not a reference, so it doesn't make sense. It should compile, but it's a little confusing. Removing that const is clearer. see this SO question for more information on that. Your parameters to isColliding should be ...


11

SFML implements functionalities that allow you to determine if two rectangles intersect. You can significantly shorten your current code: const bool Game::isColliding(const sf::RectangleShape& player, const sf::RectangleShape& enemy) const { return player.getGlobalBounds().intersects(enemy.getGlobalBounds()); } See more: sf::FloatRect::...


10

John's answer is correct on programming matters. I want to comment on the logic of your code. I found the collision detection calculation difficult to interpret. Which point on the square is represented by playerPosition? The documentation for SFML says it's the upper-left corner (although in a roundabout way), but it could also reasonably be the center. I ...


3

Some style points first. Always wrap statement and loop blocks in {} eg you wrote for (let j = 0; j < x2; j++) someFunction(j) to avoid maintenance headaches use the curlies for (let j = 0; j < x2; j++) { someFunction(j) } Don't declare the same variable over and over. There is no advantage to locally scoping variables to code blocks unless you are ...


3

Bug #1 Your code does not follow pattern (x - a < y + b) && (x + b + a > y) but (x - a < y + b1) || (x + b2 + a > y) instead. So our refactored formula is moot! obsolete Bug #2 A collision is detected when any of the guards trigger, not when all. Proposed Solution Solving both bugs, we could refactor Nikko Khresna's method: function ...


2

From a lengthy review; Clean up tags that are not used (doubles like body or unused like nav) Use a beautifier like https://beautifier.io/ Make sure you close your tags properly (the closing tag of the time section was missing) Leverage css styling, inline styling does not scale, and HTML is not the right place to style! document.getElementById is supported ...


2

I think everyone has some code they are embarrassed and not proud of and today I have decided to show mine. A pattern to get out of this mess .. That's an impressive amount of nested if-statements. As you can see, the behavior of collision handling depends both on the current instance's type and the provided instance's type. There is a pattern suited for ...


2

It is possible to refactor your code starting from the definition of your variables: int maxX = screenWidth - image.getWidth()/2; int minX = image.getWidth() / 2; int maxY = screenHeight - image.getHeight()/2; int minY = image.getHeight() / 2; You are repeating function calls image.getWidth(), image.getHeight() and divisions by 2 in more than one ...


1

Intersection logic For the intersection logic, maybe it would be appropriate to make a method on the cell class that would determine if another cell (e.g. tank) overlaps with it. Refer to this answer for inspiration. Review Just as a POJO is created for the definition of Directions I'd recommend setting up a POJO for the colors used in drawMap. That way ...


Only top voted, non community-wiki answers of a minimum length are eligible