allow decorations to be only sensor
MODIFY
lib/decoration/decoration.dart
lib/decoration/decoration.dart
@@ -28,17 +28,22 @@ class GameDecoration extends AnimatedObject with ObjectCollision {
/// World position that this decoration must position yourself.
final Position initPosition;
+ /// If set to true, won't be triggered on collision, but will trigger onContact()
+ final bool isSensor;
+
Sprite _sprite;
- GameDecoration(
- {Sprite sprite,
+ GameDecoration({
+ Sprite sprite,
@required this.initPosition,
@required this.height,
@required this.width,
this.frontFromPlayer = false,
+ this.isSensor = false,
FlameAnimation.Animation animation,
Collision collision,
- bool isTouchable = false}) {
+ bool isTouchable = false,
+ }) {
this.animation = animation;
_sprite = sprite;
this.position = generateRectWithBleedingPixel(
@@ -56,6 +61,7 @@ class GameDecoration extends AnimatedObject with ObjectCollision {
@required this.height,
@required this.width,
this.frontFromPlayer = false,
+ this.isSensor = false,
Collision collision,
bool isTouchable = false,
}) {
@@ -74,6 +80,7 @@ class GameDecoration extends AnimatedObject with ObjectCollision {
@required this.initPosition,
@required this.height,
@required this.width,
+ this.isSensor = false,
this.frontFromPlayer = false,
Collision collision,
bool isTouchable = false,
@@ -107,6 +114,8 @@ class GameDecoration extends AnimatedObject with ObjectCollision {
}
}
+ void onContact(ObjectCollision collision) { }
+
Rect generateRectWithBleedingPixel(
Position position,
double width,

MODIFY
lib/player/player.dart
lib/player/player.dart
@@ -73,6 +73,8 @@ class Player extends AnimatedObject
if (isCollision(displacement, gameRef)) return;
+ triggerSensors(displacement, gameRef);
+
position = displacement;
}
@@ -83,6 +85,8 @@ class Player extends AnimatedObject
if (isCollision(displacement, gameRef)) return;
+ triggerSensors(displacement, gameRef);
+
position = displacement;
}
@@ -94,6 +98,8 @@ class Player extends AnimatedObject
if (isCollision(displacement, gameRef)) return;
+ triggerSensors(displacement, gameRef);
+
position = displacement;
}
@@ -104,6 +110,8 @@ class Player extends AnimatedObject
if (isCollision(displacement, gameRef)) return;
+ triggerSensors(displacement, gameRef);
+
position = displacement;
}
@@ -150,6 +158,7 @@ class Player extends AnimatedObject
Rect newPosition = position.shift(newDiffBase);
if (isCollision(newPosition, gameRef)) return;
+ triggerSensors(newPosition, gameRef);
position = newPosition;

MODIFY
lib/util/collision/object_collision.dart
lib/util/collision/object_collision.dart
@@ -8,10 +8,22 @@ import 'package:flutter/material.dart';
mixin ObjectCollision {
Collision collision = Collision();
+ void triggerSensors(Rect displacement, RPGGame game) {
+ Rect rectCollision = getRectCollision(displacement);
+
+ final sensors = game.visibleDecorations().where(
+ (decoration) => decoration.isSensor,
+ );
+
+ sensors.forEach((decoration) {
+ if (decoration.rectCollision.overlaps(rectCollision)) decoration.onContact(this);
+ });
+ }
+
bool isCollision(Rect displacement, RPGGame game, {bool onlyVisible = true}) {
Rect rectCollision = getRectCollision(displacement);
- var collisions = (onlyVisible
+ final collisions = (onlyVisible
? game.map.getCollisionsRendered()
: game.map.getCollisions())
.where((i) => i.position.overlaps(rectCollision));
@@ -20,11 +32,12 @@ mixin ObjectCollision {
return true;
}
- var collisionsDecorations =
- (onlyVisible ? game.visibleDecorations() : game.decorations())
- .where((i) =>
- i.collision != null && i.rectCollision.overlaps(rectCollision))
- .cast();
+ final collisionsDecorations =
+ (onlyVisible ? game.visibleDecorations() : game.decorations()).where(
+ (i) =>
+ !i.isSensor &&
+ i.collision != null &&
+ i.rectCollision.overlaps(rectCollision));
if (collisionsDecorations.length > 0) {
return true;

allow decorations to be only sensor