From 7eb109cf925626af64c01a9a2c82693444e7571d Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Thu, 12 Aug 2021 13:50:52 +0200 Subject: [PATCH] add collision with enemies --- cart.wren | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/cart.wren b/cart.wren index 331fd0d..6e2bba3 100644 --- a/cart.wren +++ b/cart.wren @@ -326,15 +326,24 @@ class World { i = i + 1 } } + if (_player.alive) { - if (!(fgCheck(_player.x - _player.bw / 2, _player.y - _player.bh / 2) && - fgCheck(_player.x - _player.bw / 2, _player.y + _player.bh / 2) && - fgCheck(_player.x + _player.bw / 2, _player.y + _player.bh / 2) && - fgCheck(_player.x + _player.bw / 2, _player.y - _player.bh / 2))) { - //System.print("collision with foreground") + var bx = _player.x - _player.bw / 2 + var by = _player.y - _player.bh / 2 + if (!(fgCheck(bx, by) && + fgCheck(bx, by + _player.bh) && + fgCheck(bx + _player.bw, by + _player.bh) && + fgCheck(bx + _player.bw, by))) { _player.die() _vfx.add(Explosion.new(_player.x, _player.y)) } + + for (e in _enemies) { + if (collide(bx, by, _player.bw, _player.bh, e.x, e.y, e.w, e.h)) { + _player.die() + _vfx.add(Explosion.new(_player.x, _player.y)) + } + } } }