summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-03-14 16:02:40 +0800
committerEltrac <hi@guhub.cn>2025-03-14 16:02:40 +0800
commit3dab7a77c5799682bdf0517771a45ffd7a4f33ad (patch)
tree39414573f4f4ec863d144eead1a48532a1eab891
parent85bb75d557a815fb28da8c8478f32adcea1f41ef (diff)
feat: player gets knocked back getting hit by enemy
-rw-r--r--config.lua6
-rw-r--r--objects/Player.lua11
2 files changed, 15 insertions, 2 deletions
diff --git a/config.lua b/config.lua
index 22d06d2..2e95283 100644
--- a/config.lua
+++ b/config.lua
@@ -28,6 +28,7 @@ config = {
playerShootCooldown = 0.2,
player = {
+ size = 20,
punch = {
radius = 15,
angle = 120,
@@ -36,7 +37,10 @@ config = {
damageCoefficient = 10,
cooldown = 0.5
},
- size = 20,
+ knockback = {
+ force = 200,
+ duration = 0.2
+ },
},
wave = {
diff --git a/objects/Player.lua b/objects/Player.lua
index 09f3a58..d515a5f 100644
--- a/objects/Player.lua
+++ b/objects/Player.lua
@@ -40,7 +40,7 @@ function Player:setY(y)
end
function Player:move(v)
- if self.dashing and self.dashing >= G.time then return end
+ if self.knocked and self.knocked >= G.time then return end
self.moving = (v.x ~= 0 or v.y ~= 0)
self.physicsBody:setLinearVelocity(v.x, v.y)
end
@@ -148,6 +148,15 @@ function Player:onContact(other, contact)
if not (other.stunned or self:isShielded()) then
audio.play("getsHit")
table.remove(self.hearts)
+ -- knock back Player
+ local ex, ey = other.x, other.y
+ local dx, dy = self.x - ex, self.y - ey
+ local distance = math.sqrt(dx * dx + dy * dy)
+ local normEdx, normEdy = dx / distance, dy / distance
+ local knockbackX, knockbackY = normEdx * config.player.knockback.force,
+ normEdy * config.player.knockback.force
+ self.knocked = G.time + config.player.knockback.duration
+ self.physicsBody:applyLinearImpulse(knockbackX, knockbackY)
-- shield this player
self.shieldedTill = G.time + config.playerShieldTime
end