spike example and trigger sensor on isCollision
ADD
example/assets/images/itens/spikes.png
example/assets/images/itens/spikes.png
Binary files /dev/null and b/example/assets/images/itens/spikes.png differ

ADD
example/lib/decoration/spikes.dart
+import 'package:bonfire/bonfire.dart';
+import 'package:example/map/dungeon_map.dart';
+import 'package:flame/position.dart';
+
+const TIME_BETWEEN_HITS = 1.5;
+
+class Spikes extends GameDecoration {
+ final Position initPosition;
+ double _timeSinceLastHit = 0;
+
+ Spikes(this.initPosition)
+ : super.sprite(
+ Sprite('itens/spikes.png'),
+ initPosition: initPosition,
+ width: DungeonMap.tileSize / 1.5,
+ height: DungeonMap.tileSize / 1.5,
+ isSensor: true,
+ collision: Collision(
+ width: DungeonMap.tileSize / 2,
+ height: DungeonMap.tileSize / 2,
+ align: CollisionAlign.CENTER,
+ ),
+ );
+
+ @override
+ void update(double dt) {
+ if (_timeSinceLastHit > 0) {
+ _timeSinceLastHit -= dt;
+ }
+ }
+
+ @override
+ void onContact(collision) {
+ // only allows hit player after 1.5s from last hit
+ if (_timeSinceLastHit > 0) return;
+
+ // only triggers on collision with player
+ if (collision.runtimeType.toString() == 'Knight') {
+ _timeSinceLastHit = TIME_BETWEEN_HITS;
+ gameRef.player.receiveDamage(10, 1);
+ }
+ }
+}

MODIFY
example/lib/main.dart
example/lib/main.dart
@@ -61,6 +61,8 @@ class Game extends StatelessWidget implements GameListener {
background: BackgroundColorGame(Colors.blueGrey[900]),
gameController: _controller..setListener(this),
lightingColorGame: Colors.black.withOpacity(0.5),
+ constructionMode: true,
+ showCollisionArea: true,
);
}

MODIFY
example/lib/map/dungeon_map.dart
example/lib/map/dungeon_map.dart
@@ -2,6 +2,7 @@ import 'dart:math';
import 'package:bonfire/bonfire.dart';
import 'package:example/decoration/chest.dart';
+import 'package:example/decoration/spikes.dart';
import 'package:example/decoration/torch.dart';
import 'package:example/enemy/goblin.dart';
import 'package:flame/position.dart';
@@ -74,6 +75,9 @@ class DungeonMap {
static List<GameDecoration> decorations() {
return [
+ Spikes(
+ getRelativeTilePosition(7, 7),
+ ),
GameDecoration.sprite(
Sprite('itens/barrel.png'),
initPosition: getRelativeTilePosition(10, 6),

MODIFY
lib/player/player.dart
lib/player/player.dart
@@ -74,8 +74,6 @@ class Player extends AnimatedObject
if (isCollision(displacement, gameRef)) return;
- triggerSensors(displacement, gameRef);
-
position = displacement;
}
@@ -86,8 +84,6 @@ class Player extends AnimatedObject
if (isCollision(displacement, gameRef)) return;
- triggerSensors(displacement, gameRef);
-
position = displacement;
}
@@ -99,8 +95,6 @@ class Player extends AnimatedObject
if (isCollision(displacement, gameRef)) return;
- triggerSensors(displacement, gameRef);
-
position = displacement;
}
@@ -111,8 +105,6 @@ class Player extends AnimatedObject
if (isCollision(displacement, gameRef)) return;
- triggerSensors(displacement, gameRef);
-
position = displacement;
}

MODIFY
lib/util/collision/object_collision.dart
lib/util/collision/object_collision.dart
@@ -16,12 +16,19 @@ mixin ObjectCollision {
);
sensors.forEach((decoration) {
- if (decoration.rectCollision.overlaps(rectCollision)) decoration.onContact(this);
+ if (decoration.rectCollision.overlaps(rectCollision))
+ decoration.onContact(this);
});
}
- bool isCollision(Rect displacement, RPGGame game, {bool onlyVisible = true}) {
+ bool isCollision(
+ Rect displacement,
+ RPGGame game, {
+ bool onlyVisible = true,
+ bool shouldTriggerSensors = true,
+ }) {
Rect rectCollision = getRectCollision(displacement);
+ if (shouldTriggerSensors) triggerSensors(displacement, game);
final collisions = (onlyVisible
? game.map.getCollisionsRendered()

spike example and trigger sensor on isCollision