Skip to content

Commit

Permalink
Merge pull request #9 from mattjennings/dev
Browse files Browse the repository at this point in the history
referee stuff
  • Loading branch information
mattjennings authored Jul 9, 2023
2 parents 1e27bff + 6df4c44 commit 1e5ae85
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/actors/ball.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class Ball extends ex.Actor {
// scale down y velocity to account for perspective
vel.y *= 0.6

assets.snd_cleanImpact.play()
this.vel = this.vel.add(vel)
}
}
Expand Down
25 changes: 23 additions & 2 deletions src/actors/base-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ export class BasePlayer extends ex.Actor {
sprite: AsepriteResource
animations: Record<string, Animation>

isPain = false
isSprinting = false
debug = false

constructor({ sprite, debug, ...args }: BasePlayerArgs) {
super({
...args,
width: 16,
height: 12,
collisionType: ex.CollisionType.Passive,
collisionType: ex.CollisionType.Active,
collider: ex.Shape.Box(16, 12, ex.vec(0.5, 1), ex.vec(0, 12)),
...args,
})
this.body.limitDegreeOfFreedom.push(ex.DegreeOfFreedom.Rotation)
this.debug = debug ?? false
this.sprite = sprite
this.animations = {
Expand All @@ -39,6 +41,16 @@ export class BasePlayer extends ex.Actor {
Slide: this.sprite.getAnimation('Slide')!.clone(),
}
this.setAnimation('Idle')

let isPainCount = 0
this.animations.Pain.events.on('loop', (a) => {
isPainCount++

if (isPainCount > 3) {
this.isPain = false
isPainCount = 0
}
})
}

onInitialize(_engine: Engine): void {
Expand Down Expand Up @@ -69,4 +81,13 @@ export class BasePlayer extends ex.Actor {

this.vel = ex.vec(speed * Math.cos(angle), speed * Math.sin(angle))
}

hit(direction: ex.Vector) {
this.isPain = true
this.isSprinting = false
this.setAnimation('Pain')
assets.snd_clack.play()

this.vel = direction.scale(500)
}
}
38 changes: 32 additions & 6 deletions src/actors/referee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class Referee extends BasePlayer {
constructor() {
super({
sprite: assets.ase_referee,
collisionType: ex.CollisionType.Active,
})

this.animations.Punch = this.sprite.getAnimation('Punch')!
Expand All @@ -37,6 +38,10 @@ export class Referee extends BasePlayer {
this.animations.Punch.events.on('loop', () => {
this.isPunching = false
})

this.animations.Whistle.events.on('loop', () => {
this.isWhistling = false
})
}

onInitialize(_engine: Engine): void {
Expand All @@ -46,6 +51,15 @@ export class Referee extends BasePlayer {
)

this.directionQueue = new DirectionQueue(controls)

this.scene.on('reset', () => {
this.blowWhistle()
})

this.scene.on('start', () => {
this.isPunching = false
this.isWhistling = false
})
}

update(engine: Engine, delta: number): void {
Expand All @@ -55,7 +69,11 @@ export class Referee extends BasePlayer {
this.punch()
}

if (!this.isPunching) {
if (engine.input.keyboard.wasPressed(controls.whistle)) {
this.blowWhistle()
}

if (!this.isPunching && !this.isWhistling) {
const inputs = this.directionQueue.heldDirections

const isLeftHeld = inputs.includes('LEFT')
Expand Down Expand Up @@ -97,13 +115,21 @@ export class Referee extends BasePlayer {
this.setAnimation('Punch')
this.vel = ex.vec(0, 0)

const nearbyPlayers = this.scene.entities.filter(
const [player] = this.scene.entities.filter(
(entity) =>
entity instanceof TeamPlayer && entity.pos.distance(this.pos) < 20
) as TeamPlayer[]
entity !== this &&
entity instanceof BasePlayer &&
entity.pos.distance(this.pos) < 20
) as BasePlayer[]

if (nearbyPlayers.length) {
nearbyPlayers[0].hit()
if (player) {
player.hit(player.pos.sub(this.pos).normalize())
}
}

blowWhistle() {
this.isWhistling = true
this.setAnimation('Whistle')
this.vel = ex.vec(0, 0)
}
}
5 changes: 5 additions & 0 deletions src/actors/team-goalie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class TeamGoalie extends BasePlayer {
sprite: sprites[team],
})

this.body.mass = 2000
this.team = team

this.animations.BlockUp = this.sprite.getAnimation('BlockUp')!
Expand Down Expand Up @@ -73,6 +74,10 @@ export class TeamGoalie extends BasePlayer {
}

update(engine: Engine, delta: number): void {
if (this.isPain) {
this.vel = this.vel.scale(0.9)
return
}
const { top: netTop, bottom: netBottom } = this.getGoalBounds()

const goalieTop = this.pos.y
Expand Down
22 changes: 0 additions & 22 deletions src/actors/team-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,6 @@ export class TeamPlayer extends BasePlayer {
this.scene.on('start', () => {
this.isResetting = false
})

let isPainCount = 0
this.animations.Pain.events.on('loop', (a) => {
isPainCount++

if (isPainCount > 3) {
this.isPain = false
isPainCount = 0
}
})
}

update(engine: Engine, delta: number): void {
Expand Down Expand Up @@ -405,18 +395,6 @@ export class TeamPlayer extends BasePlayer {
}
}

hit() {
this.isPain = true
this.isSprinting = false
this.stamina = 0
this.setAnimation('Pain')

// git hit in random direction
const angle = random.pickOne([0, Math.PI / 2, Math.PI, (3 * Math.PI) / 2])

this.vel = ex.vec(500 * Math.cos(angle), 500 * Math.sin(angle))
}

trip() {
// this.isPain = true
// this.isSprinting = false
Expand Down
3 changes: 3 additions & 0 deletions src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const assets = {
ase_goalieBlue: new AsepriteResource('sprites/players/GOALIE_BLUE.json'),
ase_goalieRed: new AsepriteResource('sprites/players/GOALIE_RED.json'),
ase_referee: new AsepriteResource('sprites/referee/REFEREE.json'),

snd_clack: new ex.Sound('sfx/Clack.mp3'),
snd_cleanImpact: new ex.Sound('sfx/CleanImpact.mp3'),
}

class DevLoader extends ex.Loader {
Expand Down

0 comments on commit 1e5ae85

Please sign in to comment.