From 48f1f810750acf431d95c3b39001f6500ff42749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Wed, 4 Sep 2024 14:02:59 +0200 Subject: [PATCH 1/2] Fix the "activate behavior" action. --- extensions/reviewed/BoidsMovement.json | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/extensions/reviewed/BoidsMovement.json b/extensions/reviewed/BoidsMovement.json index 042b4a6c4..123ab32b8 100644 --- a/extensions/reviewed/BoidsMovement.json +++ b/extensions/reviewed/BoidsMovement.json @@ -8,7 +8,7 @@ "name": "BoidsMovement", "previewIconUrl": "https://resources.gdevelop-app.com/assets/Icons/Glyphster Pack/Master/SVG/Restaurant/Restaurant_restaurant_seafood_animal_fish.svg", "shortDescription": "Simulates flocks movement.", - "version": "0.2.2", + "version": "0.3.0", "description": [ "Simulates swarms or flocks movement following the separation, alignment, cohesion principles. The flock can be attracted to a location or avoid some obstacles.", "", @@ -30,6 +30,8 @@ "IWykYNRvhCZBN3vEgKEbBPOR3Oc2" ], "dependencies": [], + "globalVariables": [], + "sceneVariables": [], "eventsFunctions": [ { "description": "Define helper classes JavaScript code.", @@ -412,7 +414,12 @@ " * @returns {Boid} the created Boid", " */", " add(behavior) {", - " const boid = new Boid(behavior);", + " if (!behavior.__boidsExtension) {", + " behavior.__boidsExtension = {", + " boid: new Boid(behavior)", + " };", + " }", + " const boid = behavior.__boidsExtension.boid;", " this.boids.set(behavior.owner.id, boid);", " this.addToRBush(boid);", " return boid;", @@ -568,9 +575,7 @@ "const object = objects[0];\r", "const behaviorName = eventsFunctionContext.getBehaviorName(\"Behavior\");\r", "const behavior = object.getBehavior(behaviorName);\r", - "behavior.__boidsExtension = behavior.__boidsExtension || {\r", - " boid: runtimeScene.__boidsExtension.boidsManager.add(behavior),\r", - "};" + "runtimeScene.__boidsExtension.boidsManager.add(behavior);" ], "parameterObjects": "Object", "useStrict": true, From 7a3afa68ddb79cae7ae388f24ac2574184599c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Wed, 4 Sep 2024 15:08:06 +0200 Subject: [PATCH 2/2] Handle collision layers. --- extensions/reviewed/BoidsMovement.json | 260 ++++++++++++++----------- 1 file changed, 143 insertions(+), 117 deletions(-) diff --git a/extensions/reviewed/BoidsMovement.json b/extensions/reviewed/BoidsMovement.json index 123ab32b8..2b5f7bad8 100644 --- a/extensions/reviewed/BoidsMovement.json +++ b/extensions/reviewed/BoidsMovement.json @@ -31,7 +31,14 @@ ], "dependencies": [], "globalVariables": [], - "sceneVariables": [], + "sceneVariables": [ + { + "folded": true, + "name": "HasAlreadyRunThisFrame", + "type": "boolean", + "value": false + } + ], "eventsFunctions": [ { "description": "Define helper classes JavaScript code.", @@ -402,9 +409,9 @@ " */", " constructor() {", " /**", - " * @type {Map}", + " * @type {Set}", " */", - " this.boids = new Map();", + " this.boids = new Set();", " this.boidsRBush = new rbush();", " }", "", @@ -420,7 +427,7 @@ " };", " }", " const boid = behavior.__boidsExtension.boid;", - " this.boids.set(behavior.owner.id, boid);", + " this.boids.add(boid);", " this.addToRBush(boid);", " return boid;", " }", @@ -446,9 +453,10 @@ " * @param {gdjs.RuntimeBehavior} behavior", " */", " remove(behavior) {", - " this.boids.delete(behavior.owner.id);", + " const boid = behavior.__boidsExtension.boid;", + " this.boids.delete(boid);", "", - " this.removeFromRBush(behavior.__boidsExtension.boid);", + " this.removeFromRBush(boid);", " }", "", " /**", @@ -463,12 +471,14 @@ " * Move all instances.", " */", " moveAll() {", - " this.boids.forEach(boid => boid.flock());", - " this.boids.forEach(boid => {", + " for (const boid of this.boids) {", + " boid.flock();", + " }", + " for (const boid of this.boids) {", " boid.move();", " this.removeFromRBush(boid);", " this.addToRBush(boid);", - " });", + " }", " }", "", " /**", @@ -488,6 +498,7 @@ " searchArea.maxX = x + radius;", " searchArea.maxY = y + radius;", " // TODO The R-Tree should not allocate an array.", + " /** @type {Array} */", " const nearbys = this.boidsRBush.search(searchArea);", "", " const radiusSq = radius * radius;", @@ -501,6 +512,9 @@ " if (object === otherObject) {", " continue;", " }", + " if (behavior._getCollisionLayer() !== behavior._getCollisionLayer()) {", + " continue;", + " }", " const distanceSq = object.getSqDistanceToObject(otherObject);", " if (distanceSq < radiusSq) {", " foundBoids.push(behavior.__boidsExtension.boid);", @@ -620,10 +634,11 @@ "conditions": [ { "type": { - "value": "SceneVariableAsBoolean" + "value": "BooleanVariable" }, "parameters": [ - "__BoidsBehaviour_hasAlreadyRunThisFrame", + "HasAlreadyRunThisFrame", + "=", "" ] } @@ -631,21 +646,25 @@ "actions": [ { "type": { - "value": "SetSceneVariableAsBoolean" + "value": "SetBooleanVariable" }, "parameters": [ - "__BoidsBehaviour_hasAlreadyRunThisFrame", - "True" + "HasAlreadyRunThisFrame", + "True", + "" ] } ], "events": [ { "type": "BuiltinCommonInstructions::JsCode", - "inlineCode": "runtimeScene.__boidsExtension.boidsManager.moveAll();", + "inlineCode": [ + "runtimeScene.__boidsExtension.boidsManager.moveAll();\r", + "" + ], "parameterObjects": "Object", "useStrict": true, - "eventsSheetExpanded": true + "eventsSheetExpanded": false } ] } @@ -677,11 +696,12 @@ "actions": [ { "type": { - "value": "SetSceneVariableAsBoolean" + "value": "SetBooleanVariable" }, "parameters": [ - "__BoidsBehaviour_hasAlreadyRunThisFrame", - "False" + "HasAlreadyRunThisFrame", + "False", + "" ] } ] @@ -994,56 +1014,15 @@ "actions": [ { "type": { - "value": "BoidsMovement::BoidsMovement::SetPropertyTrajectoryDistance" + "value": "SetNumberVariable" }, "parameters": [ - "Object", - "Behavior", + "TrajectoryDistance", "=", "(Object.Behavior::VelocityX() * (Object.CenterY() - CenterY) + Object.Behavior::VelocityY() * (CenterX - Object.CenterX())) / Object.Behavior::Speed()" ] } - ] - }, - { - "type": "BuiltinCommonInstructions::Comment", - "color": { - "b": 109, - "g": 230, - "r": 255, - "textB": 0, - "textG": 0, - "textR": 0 - }, - "comment": "If this distance is smaller than the specified radius, the Boid must try turn in the right direction to avoid the area." - }, - { - "type": "BuiltinCommonInstructions::Standard", - "conditions": [ - { - "type": { - "value": "BoidsMovement::BoidsMovement::PropertyTrajectoryDistance" - }, - "parameters": [ - "Object", - "Behavior", - ">", - "- Radius" - ] - }, - { - "type": { - "value": "BoidsMovement::BoidsMovement::PropertyTrajectoryDistance" - }, - "parameters": [ - "Object", - "Behavior", - "<", - "Radius" - ] - } ], - "actions": [], "events": [ { "type": "BuiltinCommonInstructions::Comment", @@ -1055,82 +1034,128 @@ "textG": 0, "textR": 0 }, - "comment": "(normalX ; normalY)" + "comment": "If this distance is smaller than the specified radius, the Boid must try turn in the right direction to avoid the area." }, { "type": "BuiltinCommonInstructions::Standard", "conditions": [ { "type": { - "value": "BoidsMovement::BoidsMovement::PropertyTrajectoryDistance" + "value": "NumberVariable" }, "parameters": [ - "Object", - "Behavior", + "TrajectoryDistance", ">", - "0" + "- Radius" ] - } - ], - "actions": [ + }, { "type": { - "value": "BoidsMovement::BoidsMovement::MoveInDirection" + "value": "NumberVariable" }, "parameters": [ - "Object", - "Behavior", - "-(CenterY - Object.CenterY())", - "CenterX - Object.CenterX()", - "DecisionWeight", - "" + "TrajectoryDistance", + "<", + "Radius" ] } - ] - }, - { - "type": "BuiltinCommonInstructions::Comment", - "color": { - "b": 109, - "g": 230, - "r": 255, - "textB": 0, - "textG": 0, - "textR": 0 - }, - "comment": "(-normalX ; -normalY)" - }, - { - "type": "BuiltinCommonInstructions::Standard", - "conditions": [ + ], + "actions": [], + "events": [ { - "type": { - "value": "BoidsMovement::BoidsMovement::PropertyTrajectoryDistance" + "type": "BuiltinCommonInstructions::Comment", + "color": { + "b": 109, + "g": 230, + "r": 255, + "textB": 0, + "textG": 0, + "textR": 0 }, - "parameters": [ - "Object", - "Behavior", - "<", - "0" + "comment": "(normalX ; normalY)" + }, + { + "type": "BuiltinCommonInstructions::Standard", + "conditions": [ + { + "type": { + "value": "NumberVariable" + }, + "parameters": [ + "TrajectoryDistance", + ">", + "0" + ] + } + ], + "actions": [ + { + "type": { + "value": "BoidsMovement::BoidsMovement::MoveInDirection" + }, + "parameters": [ + "Object", + "Behavior", + "-(CenterY - Object.CenterY())", + "CenterX - Object.CenterX()", + "DecisionWeight", + "" + ] + } ] - } - ], - "actions": [ + }, { - "type": { - "value": "BoidsMovement::BoidsMovement::MoveInDirection" + "type": "BuiltinCommonInstructions::Comment", + "color": { + "b": 109, + "g": 230, + "r": 255, + "textB": 0, + "textG": 0, + "textR": 0 }, - "parameters": [ - "Object", - "Behavior", - "CenterY - Object.CenterY()", - "-(CenterX - Object.CenterX())", - "DecisionWeight", - "" + "comment": "(-normalX ; -normalY)" + }, + { + "type": "BuiltinCommonInstructions::Standard", + "conditions": [ + { + "type": { + "value": "NumberVariable" + }, + "parameters": [ + "TrajectoryDistance", + "<", + "0" + ] + } + ], + "actions": [ + { + "type": { + "value": "BoidsMovement::BoidsMovement::MoveInDirection" + }, + "parameters": [ + "Object", + "Behavior", + "CenterY - Object.CenterY()", + "-(CenterX - Object.CenterX())", + "DecisionWeight", + "" + ] + } ] } ] } + ], + "variables": [ + { + "folded": true, + "name": "TrajectoryDistance", + "type": "number", + "value": 0 + } ] } ], @@ -2190,14 +2215,15 @@ "name": "SeparationWeight" }, { - "value": "", + "value": "0", "type": "Number", - "label": "", + "unit": "Dimensionless", + "label": "Collision layer", "description": "", "group": "", "extraInformation": [], - "hidden": true, - "name": "TrajectoryDistance" + "advanced": true, + "name": "CollisionLayer" } ], "sharedPropertyDescriptors": []