2022-12-23 09:31:04 +00:00
package main
import (
2022-12-24 05:57:32 +00:00
"github.com/gopherjs/gopherjs/js"
2022-12-25 06:18:48 +00:00
. "jsexport/battle"
2022-12-25 12:17:22 +00:00
"resolv"
2022-12-23 09:31:04 +00:00
)
2023-02-16 00:17:50 +00:00
/ *
[ WARNING ] Should avoid using "MakeFullWrapper" as much as possible , and completely remove its usage in 60 fps calls like "update(dt)" on frontend !
* /
2023-02-17 04:26:07 +00:00
func NewDynamicRectangleColliders ( cnt int ) [ ] * js . Object {
ret := make ( [ ] * js . Object , cnt )
for i := 0 ; i < cnt ; i ++ {
ret [ i ] = js . MakeWrapper ( GenerateRectCollider ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , nil , "" ) )
}
return ret
}
2023-02-16 00:17:50 +00:00
func NewCollisionHolder ( ) * js . Object {
return js . MakeWrapper ( resolv . NewCollision ( ) )
}
2022-12-28 10:06:05 +00:00
func NewInputFrameDownsync ( inputFrameId int32 , inputList [ ] uint64 , confirmedList uint64 ) * js . Object {
2023-02-16 00:17:50 +00:00
return js . MakeWrapper ( & InputFrameDownsync {
2022-12-28 10:06:05 +00:00
InputFrameId : inputFrameId ,
InputList : inputList ,
ConfirmedList : confirmedList ,
} )
}
func NewRingBufferJs ( n int32 ) * js . Object {
2023-02-16 00:17:50 +00:00
return js . MakeWrapper ( resolv . NewRingBuffer ( n ) )
2022-12-28 10:06:05 +00:00
}
2022-12-24 05:57:32 +00:00
func NewCollisionSpaceJs ( spaceW , spaceH , minStepW , minStepH int ) * js . Object {
return js . MakeWrapper ( resolv . NewSpace ( spaceW , spaceH , minStepW , minStepH ) )
2022-12-23 09:31:04 +00:00
}
2022-12-24 05:57:32 +00:00
func NewVec2DJs ( x , y float64 ) * js . Object {
2023-02-16 00:17:50 +00:00
return js . MakeWrapper ( & Vec2D {
2022-12-24 05:57:32 +00:00
X : x ,
Y : y ,
} )
}
func NewPolygon2DJs ( anchor * Vec2D , points [ ] * Vec2D ) * js . Object {
2023-02-16 00:17:50 +00:00
return js . MakeWrapper ( & Polygon2D {
2022-12-24 05:57:32 +00:00
Anchor : anchor ,
Points : points ,
} )
}
func NewBarrierJs ( boundary * Polygon2D ) * js . Object {
return js . MakeWrapper ( & Barrier {
Boundary : boundary ,
} )
}
2023-02-08 08:15:05 +00:00
func NewPlayerDownsyncJs ( id , virtualGridX , virtualGridY , dirX , dirY , velX , velY , framesToRecover , framesInChState , activeSkillId , activeSkillHit , framesInvinsible , speed , battleState , characterState , joinIndex , hp , maxHp , colliderRadius int32 , inAir , onWall bool , onWallNormX , onWallNormY int32 , capturedByInertia bool , bulletTeamId , chCollisionTeamId int32 , revivalVirtualGridX , revivalVirtualGridY int32 ) * js . Object {
2023-02-14 15:27:21 +00:00
return js . MakeWrapper ( NewPlayerDownsync ( id , virtualGridX , virtualGridY , dirX , dirY , velX , velY , framesToRecover , framesInChState , activeSkillId , activeSkillHit , framesInvinsible , speed , battleState , characterState , joinIndex , hp , maxHp , colliderRadius , inAir , onWall , onWallNormX , onWallNormY , capturedByInertia , bulletTeamId , chCollisionTeamId , revivalVirtualGridX , revivalVirtualGridY ) )
2022-12-24 05:57:32 +00:00
}
2023-01-15 05:11:19 +00:00
func NewMeleeBulletJs ( bulletLocalId , originatedRenderFrameId , offenderJoinIndex , startupFrames , cancellableStFrame , cancellableEdFrame , activeFrames , hitStunFrames , blockStunFrames , pushbackVelX , pushbackVelY , damage , selfLockVelX , selfLockVelY , hitboxOffsetX , hitboxOffsetY , hitboxSizeX , hitboxSizeY int32 , blowUp bool , teamId , blState , framesInBlState , explosionFrames , speciesId int32 ) * js . Object {
return js . MakeWrapper ( NewMeleeBullet ( bulletLocalId , originatedRenderFrameId , offenderJoinIndex , startupFrames , cancellableStFrame , cancellableEdFrame , activeFrames , hitStunFrames , blockStunFrames , pushbackVelX , pushbackVelY , damage , selfLockVelX , selfLockVelY , hitboxOffsetX , hitboxOffsetY , hitboxSizeX , hitboxSizeY , blowUp , teamId , blState , framesInBlState , explosionFrames , speciesId ) )
2023-01-10 04:08:15 +00:00
}
2023-01-15 05:11:19 +00:00
func NewFireballBulletJs ( bulletLocalId , originatedRenderFrameId , offenderJoinIndex , startupFrames , cancellableStFrame , cancellableEdFrame , activeFrames , hitStunFrames , blockStunFrames , pushbackVelX , pushbackVelY , damage , selfLockVelX , selfLockVelY , hitboxOffsetX , hitboxOffsetY , hitboxSizeX , hitboxSizeY int32 , blowUp bool , teamId int32 , virtualGridX , virtualGridY , dirX , dirY , velX , velY , speed , blState , framesInBlState , explosionFrames , speciesId int32 ) * js . Object {
return js . MakeWrapper ( NewFireballBullet ( bulletLocalId , originatedRenderFrameId , offenderJoinIndex , startupFrames , cancellableStFrame , cancellableEdFrame , activeFrames , hitStunFrames , blockStunFrames , pushbackVelX , pushbackVelY , damage , selfLockVelX , selfLockVelY , hitboxOffsetX , hitboxOffsetY , hitboxSizeX , hitboxSizeY , blowUp , teamId , virtualGridX , virtualGridY , dirX , dirY , velX , velY , speed , blState , framesInBlState , explosionFrames , speciesId ) )
2022-12-29 04:21:01 +00:00
}
2023-01-08 12:34:29 +00:00
func NewNpcPatrolCue ( flAct , frAct uint64 , x , y float64 ) * js . Object {
2023-02-16 00:17:50 +00:00
return js . MakeWrapper ( & NpcPatrolCue {
2023-01-08 12:34:29 +00:00
FlAct : flAct ,
FrAct : frAct ,
X : x ,
Y : y ,
} )
}
2023-01-10 04:08:15 +00:00
func NewRoomDownsyncFrameJs ( id int32 , playersArr [ ] * PlayerDownsync , bulletLocalIdCounter int32 , meleeBullets [ ] * MeleeBullet , fireballBullets [ ] * FireballBullet ) * js . Object {
2023-02-17 10:54:51 +00:00
preallocatedRdf := NewPreallocatedRoomDownsyncFrame ( len ( playersArr ) , 64 , 64 )
CloneRoomDownsyncFrame ( id , playersArr , bulletLocalIdCounter , meleeBullets , fireballBullets , preallocatedRdf )
return js . MakeWrapper ( preallocatedRdf )
2022-12-23 09:31:04 +00:00
}
2022-12-25 06:18:48 +00:00
func GetCollisionSpaceObjsJs ( space * resolv . Space ) [ ] * js . Object {
2022-12-25 10:44:29 +00:00
// [WARNING] We couldn't just use the existing method "space.Objects()" to access them in JavaScript, there'd a stackoverflow error
2022-12-25 06:18:48 +00:00
objs := space . Objects ( )
2023-02-16 00:17:50 +00:00
ret := make ( [ ] * js . Object , len ( objs ) )
for i , obj := range objs {
ret [ i ] = js . MakeWrapper ( obj )
2022-12-25 06:18:48 +00:00
}
return ret
}
2022-12-24 11:06:31 +00:00
func GenerateConvexPolygonColliderJs ( unalignedSrc * Polygon2D , spaceOffsetX , spaceOffsetY float64 , data interface { } , tag string ) * js . Object {
2023-02-16 00:17:50 +00:00
return js . MakeWrapper ( GenerateConvexPolygonCollider ( unalignedSrc , spaceOffsetX , spaceOffsetY , data , tag ) )
2022-12-24 11:06:31 +00:00
}
2022-12-31 07:47:45 +00:00
func GetCharacterConfigsOrderedByJoinIndex ( speciesIdList [ ] int ) [ ] * js . Object {
ret := make ( [ ] * js . Object , len ( speciesIdList ) , len ( speciesIdList ) )
for i , speciesId := range speciesIdList {
2023-02-16 14:23:12 +00:00
ret [ i ] = js . MakeFullWrapper ( Characters [ speciesId ] )
2022-12-31 07:47:45 +00:00
}
return ret
}
2023-02-25 15:52:01 +00:00
func ApplyInputFrameDownsyncDynamicsOnSingleRenderFrameJs ( inputsBuffer * resolv . RingBuffer , currRenderFrameId int32 , collisionSys * resolv . Space , collisionSysMap map [ int32 ] * resolv . Object , collisionSpaceOffsetX , collisionSpaceOffsetY float64 , chConfigsOrderedByJoinIndex [ ] * CharacterConfig , renderFrameBuffer * resolv . RingBuffer , collision * resolv . Collision , effPushbacks [ ] * Vec2D , hardPushbackNormsArr [ ] [ ] * Vec2D , jumpedOrNotList [ ] bool , dynamicRectangleColliders [ ] * resolv . Object , lastIndividuallyConfirmedInputFrameId [ ] int32 , lastIndividuallyConfirmedInputList [ ] uint64 , allowUpdateInputFrameInPlaceUponDynamics bool , toExcludeJoinIndexUpdateInputFrameInPlaceUponDynamics int32 ) bool {
2022-12-24 05:57:32 +00:00
// We need access to all fields of RoomDownsyncFrame for displaying in frontend
2023-02-25 15:52:01 +00:00
return ApplyInputFrameDownsyncDynamicsOnSingleRenderFrame ( inputsBuffer , currRenderFrameId , collisionSys , collisionSysMap , collisionSpaceOffsetX , collisionSpaceOffsetY , chConfigsOrderedByJoinIndex , renderFrameBuffer , collision , effPushbacks , hardPushbackNormsArr , jumpedOrNotList , dynamicRectangleColliders , lastIndividuallyConfirmedInputFrameId , lastIndividuallyConfirmedInputList , allowUpdateInputFrameInPlaceUponDynamics , toExcludeJoinIndexUpdateInputFrameInPlaceUponDynamics )
2022-12-23 09:31:04 +00:00
}
2023-02-16 14:23:12 +00:00
func GetRoomDownsyncFrame ( renderFrameBuffer * resolv . RingBuffer , frameId int32 ) * js . Object {
// [WARNING] Calling "renderFrameBuffer.GetByFrameId(frameId)" directly from transpiled frontend code would automatically invoke the expensive "$externalize" and "$mapArray"! See profiling result for more details.
candidate := renderFrameBuffer . GetByFrameId ( frameId )
if nil == candidate {
return nil
}
return js . MakeWrapper ( candidate . ( * RoomDownsyncFrame ) )
}
func GetInputFrameDownsync ( inputsBuffer * resolv . RingBuffer , inputFrameId int32 ) * js . Object {
candidate := inputsBuffer . GetByFrameId ( inputFrameId )
if nil == candidate {
return nil
}
return js . MakeWrapper ( candidate . ( * InputFrameDownsync ) )
}
func GetInput ( ifd * InputFrameDownsync , i int ) uint64 {
// [WARNING] Calling "ifd.GetInputList()" directly from transpiled frontend code would make a copy of the array.
return ifd . InputList [ i ]
}
2023-02-17 10:54:51 +00:00
func SetInputFrameId ( ifd * InputFrameDownsync , newVal int32 ) bool {
// [WARNING] This function should be only used by frontend which is single-threaded; on the backend more rigorous thread-safety concerns are taken care of by proper locking.
ifd . InputFrameId = newVal
return true
}
func SetInput ( ifd * InputFrameDownsync , i int , newVal uint64 ) bool {
// [WARNING] This function should be only used by frontend which is single-threaded; on the backend more rigorous thread-safety concerns are taken care of by proper locking.
if i >= len ( ifd . InputList ) {
return false
}
ifd . InputList [ i ] = newVal
return true
}
func SetConfirmedList ( ifd * InputFrameDownsync , newVal uint64 ) bool {
// [WARNING] This function should be only used by frontend which is single-threaded; on the backend more rigorous thread-safety concerns are taken care of by proper locking.
ifd . ConfirmedList = newVal
return true
}
2023-02-16 14:23:12 +00:00
func GetPlayer ( rdf * RoomDownsyncFrame , i int ) * js . Object {
// [WARNING] Calling "rdf.GetPlayersArr()" directly from transpiled frontend code would automatically invoke the expensive "$externalize" and "$mapArray"! See profiling result for more details.
return js . MakeWrapper ( rdf . PlayersArr [ i ] )
}
func GetMeleeBullet ( rdf * RoomDownsyncFrame , i int ) * js . Object {
if TERMINATING_BULLET_LOCAL_ID == rdf . MeleeBullets [ i ] . GetBulletLocalId ( ) {
return nil
}
return js . MakeWrapper ( rdf . MeleeBullets [ i ] )
}
func GetFireballBullet ( rdf * RoomDownsyncFrame , i int ) * js . Object {
if TERMINATING_BULLET_LOCAL_ID == rdf . FireballBullets [ i ] . GetBulletLocalId ( ) {
return nil
}
return js . MakeWrapper ( rdf . FireballBullets [ i ] )
}
2022-12-23 09:31:04 +00:00
func main ( ) {
js . Global . Set ( "gopkgs" , map [ string ] interface { } {
2023-02-16 00:17:50 +00:00
"NewVec2DJs" : NewVec2DJs ,
"NewPolygon2DJs" : NewPolygon2DJs ,
"NewBarrierJs" : NewBarrierJs ,
"NewPlayerDownsyncJs" : NewPlayerDownsyncJs ,
"NewMeleeBulletJs" : NewMeleeBulletJs ,
"NewFireballBulletJs" : NewFireballBulletJs ,
"NewNpcPatrolCue" : NewNpcPatrolCue ,
"NewRoomDownsyncFrameJs" : NewRoomDownsyncFrameJs ,
"NewCollisionSpaceJs" : NewCollisionSpaceJs ,
2023-02-16 04:51:46 +00:00
"NewCollisionHolder" : NewCollisionHolder ,
2023-02-16 00:17:50 +00:00
"NewInputFrameDownsync" : NewInputFrameDownsync ,
"NewRingBufferJs" : NewRingBufferJs ,
"GenerateConvexPolygonColliderJs" : GenerateConvexPolygonColliderJs ,
"GetCollisionSpaceObjsJs" : GetCollisionSpaceObjsJs ,
"WorldToPolygonColliderBLPos" : WorldToPolygonColliderBLPos , // No need to wrap primitive return types
"PolygonColliderBLToWorldPos" : PolygonColliderBLToWorldPos ,
"WorldToVirtualGridPos" : WorldToVirtualGridPos ,
"VirtualGridToWorldPos" : VirtualGridToWorldPos ,
"GetCharacterConfigsOrderedByJoinIndex" : GetCharacterConfigsOrderedByJoinIndex ,
2023-01-01 07:43:25 +00:00
"ApplyInputFrameDownsyncDynamicsOnSingleRenderFrameJs" : ApplyInputFrameDownsyncDynamicsOnSingleRenderFrameJs ,
2023-01-02 15:35:56 +00:00
"ConvertToDelayedInputFrameId" : ConvertToDelayedInputFrameId ,
"ConvertToNoDelayInputFrameId" : ConvertToNoDelayInputFrameId ,
"ConvertToFirstUsedRenderFrameId" : ConvertToFirstUsedRenderFrameId ,
"ConvertToLastUsedRenderFrameId" : ConvertToLastUsedRenderFrameId ,
"ShouldGenerateInputFrameUpsync" : ShouldGenerateInputFrameUpsync ,
2023-02-16 00:17:50 +00:00
"IsGeneralBulletActive" : IsGeneralBulletActive ,
2023-02-16 14:23:12 +00:00
"GetRoomDownsyncFrame" : GetRoomDownsyncFrame ,
"GetInputFrameDownsync" : GetInputFrameDownsync ,
"GetPlayer" : GetPlayer ,
"GetMeleeBullet" : GetMeleeBullet ,
"GetFireballBullet" : GetFireballBullet ,
"GetInput" : GetInput ,
2023-02-17 04:26:07 +00:00
"NewDynamicRectangleColliders" : NewDynamicRectangleColliders ,
2023-02-17 10:54:51 +00:00
"SetInputFrameId" : SetInputFrameId ,
"SetInput" : SetInput ,
"SetConfirmedList" : SetConfirmedList ,
2022-12-23 09:31:04 +00:00
} )
}