Skip to content

Commit

Permalink
Fixed directions while attacking
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikketer committed Jul 13, 2024
1 parent 6f733bc commit 239a527
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
12 changes: 9 additions & 3 deletions island.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ namespace Island {
if (currentSegment < (_island.segments - 1)) {
// Tiny delay to show the arrow, cuz... TMNT
setTimeout(() => {
arrow = sprites.create(assets.image`Arrow`)
arrow.x = 140
arrow.y = 80
// Recheck if complete just in case we walked before this appeared
if (isSegmentComplete) {
arrow = sprites.create(assets.image`Arrow`)
arrow.x = 140
arrow.y = 80
}
}, 1500)
}
}
Expand Down Expand Up @@ -99,11 +102,14 @@ namespace Island {

function placeEnemies() {
// The number of enemies is based on the risk level of the island
// number of players AND segment level
// Start most enemies a bit from the left (avoiding starting ON the players)
const locX = Math.randomRange(_boundingBox[0] + 20, _boundingBox[2])
const locY = Math.randomRange(_boundingBox[1], _boundingBox[3])
const randomTarget = Math.randomRange(0,1) === 0 ? player1 : player2

const numberOfEnemies = Math.floor((2 * currentSegment) * _island.risk + 4)

currentEnemies.push(new Militia({ x: locX, y: locY, target: randomTarget }))
}

Expand Down
38 changes: 25 additions & 13 deletions militia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Militia {
static walkLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Militia Walk`)
static attackRightAnimation: Image[] = assets.animation`Militia Shoot`
static attackLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Militia Shoot`)
static parrySound: music.SoundEffect = music.createSoundEffect(WaveShape.Noise, 5000, 5000, 255, 0, 100, SoundExpressionEffect.Vibrato, InterpolationCurve.Curve)
static speed: number = 10
static directionChangeInterval: number = 1000
static attackDelayMin: number = 4000
Expand All @@ -11,15 +12,18 @@ class Militia {
sprite: Sprite
currentTarget?: Pirate
facing: 'left' | 'right' = 'right'
_nextAttackTime: number = 4000
_lastAttackTick: number = 0
_nextAttackTime: number
_lastAttackTick: number
_lastDirectionTick: number = 0
public isParrying: boolean = false
_isAttacking: boolean = false
_isParrying: boolean = false
public health: number = 1

constructor({ x, y, target }: { x: number, y: number, target?: Pirate }) {
this.sprite = sprites.create(assets.animation`Militia Walk`[0])
this.place(x, y)
this._nextAttackTime = 4000
this._lastAttackTick = 0

this.currentTarget = target

Expand All @@ -32,7 +36,11 @@ class Militia {
}

public hit(damage: number) {
if (this.isParrying) return
if (this._isParrying) {
music.play(Militia.parrySound, music.PlaybackMode.InBackground)
return
}

this.health -= damage

if (this.health <= 0) {
Expand All @@ -52,18 +60,15 @@ class Militia {
this._nextAttackTime = Math.randomRange(Militia.attackDelayMin, Militia.attackDelayMax)
this.attack()
}
// Check your distance from the target randomly
if ((control.millis() - this._lastDirectionTick) > Militia.directionChangeInterval) {
this._lastDirectionTick = control.millis()
console.log('Checking target')
}
// Check your distance from the target randomly (TODO)
// if ((control.millis() - this._lastDirectionTick) > Militia.directionChangeInterval) {
// this._lastDirectionTick = control.millis()
// }

// Face your target
if (this.currentTarget.sprite.x < this.sprite.x && this.facing === 'right') {
// Turn around
if (this.currentTarget.sprite.x < this.sprite.x && this.facing === 'right' && !this._isAttacking) {
this.walk('left')
} else if (this.currentTarget.sprite.x > this.sprite.x && this.facing === 'left') {
// Turn around
} else if (this.currentTarget.sprite.x > this.sprite.x && this.facing === 'left' && !this._isAttacking) {
this.walk('right')
}
this.sprite.z = this.sprite.y
Expand Down Expand Up @@ -93,6 +98,7 @@ class Militia {
attack() {
// Stop moving
this.sprite.follow(this.currentTarget.sprite, 0)
this._isAttacking = true
// Play the fire animation
if (this.facing === 'right') {
animation.runImageAnimation(
Expand All @@ -112,6 +118,11 @@ class Militia {

// Slightly after the animation we check to see if we hit
setTimeout(() => {
// bigCrash or sonar....
music.play(music.melodyPlayable(music.bigCrash), music.PlaybackMode.InBackground)
// music.play(music.melodyPlayable(music.sonar), music.playSound)
// music.createSoundEffect(WaveShape.Sawtooth, 500, 500, 100, 0, 1000, SoundExpressionEffect.None, InterpolationCurve.Linear)
// music.playSound('1 1 1')
// Check to see that our target is in range and fire the hit
if (Math.abs(this.sprite.y - this.currentTarget.sprite.y) < 30) {
this.currentTarget.hit(this, 1)
Expand All @@ -120,6 +131,7 @@ class Militia {

// Resume walking
setTimeout(() => {
this._isAttacking = false
this.walk()
}, Militia.attackRightAnimation.length * 100)

Expand Down
10 changes: 9 additions & 1 deletion pirate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type AttackCallbackParams = { pirate: Pirate, direction: 'left' | 'right' }
class Pirate {
static _attackDelay: number = 400

static parrySound: music.SoundEffect = music.createSoundEffect(WaveShape.Noise, 5000, 5000, 255, 0, 100, SoundExpressionEffect.Vibrato, InterpolationCurve.Curve)
static deathSound: music.SoundEffect = music.createSoundEffect(WaveShape.Triangle, 2202, 476, 129, 0, 861, SoundExpressionEffect.Warble, InterpolationCurve.Logarithmic)

idleRightAnimation: Image[] = assets.animation`Pirate Stand`
idleLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Pirate Stand`)
attackLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Pirate Swing w Sword`)
Expand Down Expand Up @@ -93,7 +96,10 @@ class Pirate {
}

public hit(enemy: Militia, damage: number) {
if (this.isParrying) return
if (this.isParrying) {
music.play(Pirate.parrySound, music.PlaybackMode.InBackground)
return
}

scene.cameraShake(2, 500)
this.health -= damage
Expand All @@ -108,6 +114,8 @@ class Pirate {
// Can't attack more frequently than attackDelay
if (control.millis() - this._lastAttackTick < Pirate._attackDelay) return

music.play(Pirate.deathSound, music.PlaybackMode.InBackground)

// Clear the "is attacking" tag after the animation completes
clearTimeout(this._isAttackingTimeout)
this._isAttackingTimeout = setTimeout(() => {}, Pirate._attackDelay)
Expand Down

0 comments on commit 239a527

Please sign in to comment.