@@ -46,8 +46,8 @@ export default class Actions {
4646 //this.actions.splice(i, 1)
4747
4848 // Otherwise, this is faster
49- this . actions [ i ] = this . actions [ this . actions . length - 1 ]
50- this . actions [ i ] . finish ( round )
49+ [ this . actions [ i ] , this . actions [ this . actions . length - 1 ] ] = [ this . actions [ this . actions . length - 1 ] , this . actions [ i ] ]
50+ this . actions [ this . actions . length - 1 ] . finish ( round )
5151 this . actions . pop ( )
5252
5353 i --
@@ -109,22 +109,23 @@ export const ACTION_DEFINITIONS: Record<schema.Action, typeof Action<ActionUnion
109109 const random2 = ( ( src . pos . x * 259 + src . pos . y * 429 + match . currentRound . roundNumber * 224 ) / 100 ) % 1
110110 const interpolationFactor = match . getInterpolationFactor ( )
111111
112- ctx . save ( )
113- ctx . globalAlpha = 0.5 - 0.5 * interpolationFactor * interpolationFactor
114- ctx . fillStyle = '#000000'
115- ctx . font = '0.4px Arial'
116- // parabolic trajectory.
117- const fontX = coords . x + ( 4 * random1 - 2 ) * interpolationFactor - 0.5
118- const fontY =
119- coords . y - ( 2 + 4 * random2 ) * interpolationFactor + 8 * interpolationFactor * interpolationFactor - 0.5
120- ctx . fillText ( 'nom' , fontX , fontY )
112+ // ctx.save()
113+ // ctx.globalAlpha = 0.5 - 0.5 * interpolationFactor * interpolationFactor
114+ // ctx.fillStyle = '#000000'
115+ // ctx.font = '0.4px Arial'
116+ // // parabolic trajectory.
117+ // const fontX = coords.x + (4 * random1 - 2) * interpolationFactor - 0.5
118+ // const fontY =
119+ // coords.y - (2 + 4 * random2) * interpolationFactor + 8 * interpolationFactor * interpolationFactor - 0.5
120+ // ctx.fillText('nom', fontX, fontY)
121+ src . textureOverride = true
121122 src . imgPath = `robots/cat/cat_feed_${ src . direction } .png` // is reset in `finish`.
122- ctx . restore ( )
123+ // ctx.restore()
123124 }
124125
125126 finish ( round : Round ) : void {
126127 const src = round . bodies . getById ( this . robotId )
127- src . imgPath = 'robots/cat/cat.png'
128+ src . textureOverride = false
128129 }
129130 } ,
130131 [ schema . Action . RatAttack ] : class AttackAction extends Action < schema . RatAttack > {
@@ -183,18 +184,30 @@ export const ACTION_DEFINITIONS: Record<schema.Action, typeof Action<ActionUnion
183184 }
184185 } ,
185186 [ schema . Action . RatNap ] : class RatNapAction extends Action < schema . RatNap > {
186- private static readonly OFFSET = { x : - 0.35 , y : 0 }
187+ // private static readonly OFFSET = { x: -0.35, y: 0 }
187188 apply ( round : Round ) : void {
188189 // move the target onto the source adjust target's size using scale factor
189190 const src = round . bodies . getById ( this . robotId )
190191 const target = round . bodies . getById ( this . actionData . id ( ) ) // rat getting napped
191192
192- target . carriedRobot = undefined
193- src . carriedRobot = target . id
194-
195- target . lastPos = { ...target . pos }
196- target . pos = { x : src . pos . x + RatNapAction . OFFSET . x , y : src . pos . y + RatNapAction . OFFSET . y }
197- target . size = 0.6
193+ if ( target . beingCarried ) {
194+ // drop the target
195+ const carrier = round . bodies . getById ( target . carrierRobot ! )
196+ carrier . carriedRobot = undefined
197+ target . size = 1
198+ target . beingCarried = false
199+ target . carrierRobot = undefined
200+ } else {
201+ // pick up the target
202+ src . carriedRobot = target . id
203+ target . carrierRobot = src . id
204+ target . carriedRobot = undefined
205+ target . beingCarried = true
206+
207+ target . lastPos = { ...target . pos }
208+ // target.pos = { x: src.pos.x + RatNapAction.OFFSET.x, y: src.pos.y + RatNapAction.OFFSET.y }
209+ target . size = 0.6
210+ }
198211 }
199212 draw ( match : Match , ctx : CanvasRenderingContext2D ) : void {
200213 //target rat moves onto src rat, circle around carried group thing
@@ -219,7 +232,6 @@ export const ACTION_DEFINITIONS: Record<schema.Action, typeof Action<ActionUnion
219232 ctx . arcTo ( srcCoords . x - half , srcCoords . y - half , srcCoords . x + half , srcCoords . y - half , radius )
220233 ctx . stroke ( )
221234 ctx . restore ( )
222- ctx . restore ( )
223235 }
224236 } ,
225237 [ schema . Action . RatCollision ] : class RatCollisionAction extends Action < schema . RatCollision > {
@@ -377,10 +389,14 @@ export const ACTION_DEFINITIONS: Record<schema.Action, typeof Action<ActionUnion
377389 [ schema . Action . CatScratch ] : class CatScratchAction extends Action < schema . CatScratch > {
378390 draw ( match : Match , ctx : CanvasRenderingContext2D ) : void {
379391 // cat scratching animation
380-
381392 const body = match . currentRound . bodies . getById ( this . robotId )
382393 const pos = match . map . indexToLocation ( this . actionData . loc ( ) )
383394 const coords = renderUtils . getRenderCoords ( pos . x , pos . y , match . map . dimension , true )
395+
396+ const dir = body . direction
397+ body . textureOverride = true
398+ body . imgPath = `robots/cat/cat_scratch_${ dir } .png`
399+
384400 const reflected = body . pos . x < pos . x
385401
386402 const interpolationFactor = match . getInterpolationFactor ( )
@@ -399,12 +415,11 @@ export const ACTION_DEFINITIONS: Record<schema.Action, typeof Action<ActionUnion
399415 }
400416 ctx . stroke ( )
401417 ctx . globalAlpha = 1
402- body . imgPath = `robots/cat/cat_scratch_${ body . direction } .png`
403418 }
404419
405420 finish ( round : Round ) : void {
406421 const body = round . bodies . getById ( this . robotId )
407- body . imgPath = 'robots/cat/cat.png'
422+ body . textureOverride = false
408423 }
409424 } ,
410425 [ schema . Action . CatPounce ] : class CatPounceAction extends Action < schema . CatPounce > {
@@ -413,6 +428,7 @@ export const ACTION_DEFINITIONS: Record<schema.Action, typeof Action<ActionUnion
413428 const body = round . bodies . getById ( this . robotId )
414429 const startPos = round . map . indexToLocation ( this . actionData . startLoc ( ) )
415430 const endPos = round . map . indexToLocation ( this . actionData . endLoc ( ) )
431+ console . log ( 'pounce from' , startPos , 'to' , endPos )
416432 }
417433 draw ( match : Match , ctx : CanvasRenderingContext2D ) : void {
418434 // cat pouncing animation
@@ -423,6 +439,7 @@ export const ACTION_DEFINITIONS: Record<schema.Action, typeof Action<ActionUnion
423439 const endCoords = renderUtils . getRenderCoords ( endPos . x , endPos . y , match . map . dimension , true )
424440 const angle = Math . atan2 ( endPos . y - startPos . y , endPos . x - startPos . x )
425441
442+ body . textureOverride = true
426443 let texture : string
427444 if ( angle >= ( 7 * Math . PI ) / 4 || angle <= Math . PI / 4 ) {
428445 texture = 'robots/cat/cat_pounce_5.png'
@@ -446,7 +463,7 @@ export const ACTION_DEFINITIONS: Record<schema.Action, typeof Action<ActionUnion
446463 }
447464 finish ( round : Round ) : void {
448465 const body = round . bodies . getById ( this . robotId )
449- body . imgPath = 'robots/cat/cat.png'
466+ body . textureOverride = false
450467 }
451468 } ,
452469 [ schema . Action . PlaceTrap ] : class PlaceTrapAction extends Action < schema . PlaceTrap > {
@@ -500,7 +517,8 @@ export const ACTION_DEFINITIONS: Record<schema.Action, typeof Action<ActionUnion
500517 draw ( match : Match , ctx : CanvasRenderingContext2D ) : void {
501518 // trap triggering animation
502519 const body = match . currentRound . bodies . getById ( this . robotId )
503- const pos = match . map . indexToLocation ( this . actionData . loc ( ) )
520+ // const pos = match.map.indexToLocation(this.actionData.loc())
521+ const pos = body . getInterpolatedCoords ( match )
504522 const coords = renderUtils . getRenderCoords ( pos . x , pos . y , match . map . dimension , true )
505523
506524 const size = body . size - 1
@@ -546,6 +564,14 @@ export const ACTION_DEFINITIONS: Record<schema.Action, typeof Action<ActionUnion
546564 // maybe move rat to target loc
547565 const body = round . bodies . getById ( this . robotId )
548566 const endLoc = round . map . indexToLocation ( this . actionData . loc ( ) )
567+ if ( body . carriedRobot !== undefined ) {
568+ const carrier = round . bodies . getById ( body . carriedRobot )
569+ carrier . carriedRobot = undefined
570+ }
571+ body . carrierRobot = undefined
572+ body . beingCarried = false
573+ body . size = 1
574+ body . pos = { ...endLoc }
549575 }
550576 draw ( match : Match , ctx : CanvasRenderingContext2D ) : void {
551577 const body = match . currentRound . bodies . getById ( this . robotId )
@@ -555,15 +581,15 @@ export const ACTION_DEFINITIONS: Record<schema.Action, typeof Action<ActionUnion
555581
556582 const from = pos
557583 const to = match . currentRound . map . indexToLocation ( this . actionData . loc ( ) )
558-
584+ console . log ( 'throw from' , from , 'to' , to )
559585 const dx = to . x - from . x
560586 const dy = to . y - from . y
561587 const mag = Math . hypot ( dx , dy )
562588 if ( mag < 1e-3 ) return
563589
564590 const ux = dx / mag
565591 const uy = dy / mag
566- const px = - uy
592+ const px = uy
567593 const py = ux
568594
569595 // deterministic jitter
@@ -583,10 +609,10 @@ export const ACTION_DEFINITIONS: Record<schema.Action, typeof Action<ActionUnion
583609 const offset = i * spacing
584610 const jitter = ( r - 0.5 ) * 0.15
585611
586- const endX = coords . x - ux * 0.3 + px * offset + px * jitter
612+ const endX = coords . x + ux * 0.3 + px * offset + px * jitter
587613 const endY = coords . y - uy * 0.3 + py * offset + py * jitter
588614
589- const startX = endX - ux * baseLength
615+ const startX = endX + ux * baseLength
590616 const startY = endY - uy * baseLength
591617
592618 ctx . beginPath ( )
@@ -655,16 +681,15 @@ export const ACTION_DEFINITIONS: Record<schema.Action, typeof Action<ActionUnion
655681 draw ( match : Match , ctx : CanvasRenderingContext2D ) : void {
656682 const body = match . currentRound . bodies . getById ( this . robotId )
657683 const renderCoords = renderUtils . getRenderCoords (
658- body . pos . x - 1 + body . size / 2 ,
659- body . pos . y + body . size / 2 ,
684+ body . pos . x ,
685+ body . pos . y ,
660686 match . map . dimension ,
661- true
662687 )
663688 renderUtils . renderCenteredImageOrLoadingIndicator (
664689 ctx ,
665690 getImageIfLoaded ( 'robots/squeak.png' ) ,
666691 renderCoords ,
667- 1
692+ body . size
668693 )
669694 }
670695 } ,
0 commit comments