Bonjour, je cherche a faire un jeu dans le genre des lemmings donc un jeu utilisant la destruction du décors. J’ai bien regardé l’exemple dans Gdevelop. Ma question est comment créer le décors pour utilisé la fonction avec MarchingSquaresBehavior et Mask.
davy
October 27, 2022, 10:17pm
2
J’avais commencé un exemple d’éditeur de niveau mais l’outil pour enlever de la matière n’est pas assez précis. Il faut que je trouve une meilleur formule. Il faudrait sans doute aussi que j’ajoute des actions pour dessiner avec des courbes de Bézier.
Je ne sais pas quand j’aurai le temps d’améliorer cette extension. C’est un sujet qui m’intéresse mais je travaille sur d’autres extensions en ce moment. Le projet peut être téléchargé sur cette page :
GDevelopApp:main
← D8H:MarchingSquareCollOpti
opened 05:38PM - 06 Feb 22 UTC
Also add:
* upside-down flood action
* group definitions
* fix the wiki link
…
# Example
## QIX
* Build: https://liluo.io/games/645ed2ca-2940-47e6-bd0a-df726cfa05c1
* Project: https://www.dropbox.com/s/121lrua9tws08l4/marching-squares-qix.zip?dl=1
## Platformer
* Build: https://liluo.io/games/91e4a59a-7743-4e2c-b1c3-dac7e157df30
* Project: https://www.dropbox.com/s/lbybhiii0uksrvy/marching-squares-editor.zip?dl=1
![MarchingSquareLevel](https://user-images.githubusercontent.com/2611977/152694138-867bec4b-3899-4e03-8df7-79fb863be0da.png)
# Optimization
The scenario is not favorable to the optimization:
* Small cells of 10 pixels
* A medium size map of 1600x1200
* a character with a sprite a bit bigger than its hitbox
* the character is against a wall
The character AABB is overlapping about 10 hitboxes (it could 3 or 4 in more common scenario). There is still an overall 25% CPU time save.
## Before
![CollisionOptimizationCpuBefore](https://user-images.githubusercontent.com/2611977/152692499-2276b372-cafe-4387-bec2-e0a6fa76df4d.png)
![CollisionOptimizationBottomUpBefore](https://user-images.githubusercontent.com/2611977/152692516-47507417-d01f-4639-9c23-eb7f091a2606.png)
## After
![CollisionOptimizationCpuAfter](https://user-images.githubusercontent.com/2611977/152787576-07b2364f-43ca-4bf5-9999-1adeedb7f293.png)
![CollisionOptimizationBottomUpAfter](https://user-images.githubusercontent.com/2611977/152787600-431410f4-857d-40a6-a1ab-7a01ff3704f8.png)
# JavaScript main changes
This is to only check collision with the platform hitboxes next to the other object.
## Iterable over hitboxes of a given area
```JavaScript
if (!gdjs.__marchingSquares) {
// Create a namespace
gdjs.__marchingSquares = {};
/**
* The hitboxes in a rectangular area.
* @constructor
* @param {any} behavior
* @param {integer} xMin The fist column to include.
* @param {integer} yMin The fist row to include.
* @param {integer} xMax The last column to include.
* @param {integer} yMax The last row to include.
*/
gdjs.__marchingSquares.HitboxesIterable = function HitboxesIterable(
behavior,
xMin,
yMin,
xMax,
yMax
) {
this.behavior = behavior;
this.xMin = xMin;
this.yMin = yMin;
this.xMax = xMax;
this.yMax = yMax;
};
gdjs.__marchingSquares.HitboxesIterable.prototype.setBounds = function (
xMin,
yMin,
xMax,
yMax
) {
this.xMin = xMin;
this.yMin = yMin;
this.xMax = xMax;
this.yMax = yMax;
}
gdjs.__marchingSquares.HitboxesIterable.prototype[Symbol.iterator] = function () {
// xMin and yMin next increment
let x = this.xMax;
let y = this.yMin - 1;
let hitbox = null;
let previousHitbox = null;
/** @type {IteratorReturnResult<gdjs.Polygon>} */
const result = {done: false,value: undefined};
return {
next: () => {
do {
x++;
if (x > this.xMax) {
y++;
x = this.xMin;
}
if (y > this.yMax) {
// done
result.done = true;
result.value = undefined;
return result;
}
hitbox = this.behavior.getHitbox(x, y);
}
while (!hitbox || hitbox === previousHitbox);
// happen with run-length encoding
previousHitbox = hitbox;
result.done = false;
result.value = hitbox;
return result;
},
};
};
}
```
## Overriding of object methods
```JavaScript
/**
* @type {Iterable<gdjs.Polygon>}
*/
behavior.marchingSquaresHitBoxesIterable = new gdjs.__marchingSquares.HitboxesIterable(behavior, 0, 0, 0, 0);
behavior.owner.updateHitBoxes = function () {
this.hitBoxesDirty = false;
}
behavior.owner.getHitBoxesAround = function (
left,
top,
right,
bottom
) {
const leftIndex = Math.max(0, Math.floor(behavior.convertToGridBasisX(left)));
const topIndex = Math.max(0, Math.floor(behavior.convertToGridBasisY(top)));
const rightIndex = Math.min(behavior.dimX() - 1, Math.ceil(behavior.convertToGridBasisX(right)));
const bottomIndex = Math.min(behavior.dimY() - 1, Math.ceil(behavior.convertToGridBasisY(bottom)));
/** @type {Iterable<gdjs.Polygon>} */
const iterable = behavior.marchingSquaresHitBoxesIterable;
iterable.setBounds(leftIndex, topIndex, rightIndex, bottomIndex);
return iterable;
}
```