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
)
2022-12-28 10:06:05 +00:00
func NewInputFrameDownsync ( inputFrameId int32 , inputList [ ] uint64 , confirmedList uint64 ) * js . Object {
return js . MakeFullWrapper ( & InputFrameDownsync {
InputFrameId : inputFrameId ,
InputList : inputList ,
ConfirmedList : confirmedList ,
} )
}
func NewRingBufferJs ( n int32 ) * js . Object {
return js . MakeFullWrapper ( NewRingBuffer ( n ) )
}
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 {
return js . MakeFullWrapper ( & Vec2D {
X : x ,
Y : y ,
} )
}
func NewPolygon2DJs ( anchor * Vec2D , points [ ] * Vec2D ) * js . Object {
return js . MakeFullWrapper ( & Polygon2D {
Anchor : anchor ,
Points : points ,
} )
}
func NewBarrierJs ( boundary * Polygon2D ) * js . Object {
return js . MakeWrapper ( & Barrier {
Boundary : boundary ,
} )
}
2023-01-20 03:29:27 +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 ) * js . Object {
2022-12-24 05:57:32 +00:00
return js . MakeWrapper ( & PlayerDownsync {
2023-01-08 12:34:29 +00:00
Id : id ,
VirtualGridX : virtualGridX ,
VirtualGridY : virtualGridY ,
DirX : dirX ,
DirY : dirY ,
VelX : velX ,
VelY : velY ,
FramesToRecover : framesToRecover ,
FramesInChState : framesInChState ,
ActiveSkillId : activeSkillId ,
ActiveSkillHit : activeSkillHit ,
FramesInvinsible : framesInvinsible ,
Speed : speed ,
BattleState : battleState ,
CharacterState : characterState ,
JoinIndex : joinIndex ,
Hp : hp ,
MaxHp : maxHp ,
ColliderRadius : colliderRadius ,
InAir : inAir ,
2023-01-11 23:22:14 +00:00
OnWall : onWall ,
2023-01-13 03:25:20 +00:00
OnWallNormX : onWallNormX ,
OnWallNormY : onWallNormY ,
2023-01-20 03:29:27 +00:00
CapturedByInertia : capturedByInertia ,
2023-01-08 12:34:29 +00:00
BulletTeamId : bulletTeamId ,
ChCollisionTeamId : chCollisionTeamId ,
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 {
return js . MakeFullWrapper ( & NpcPatrolCue {
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 {
2022-12-25 10:44:29 +00:00
// [WARNING] Avoid using "pb.RoomDownsyncFrame" here, in practive "MakeFullWrapper" doesn't expose the public fields for a "protobuf struct" as expected and requires helper functions like "GetCollisionSpaceObjsJs".
2022-12-24 05:57:32 +00:00
return js . MakeFullWrapper ( & RoomDownsyncFrame {
2023-01-10 04:08:15 +00:00
Id : id ,
PlayersArr : playersArr ,
BulletLocalIdCounter : bulletLocalIdCounter ,
MeleeBullets : meleeBullets ,
FireballBullets : fireballBullets ,
2022-12-24 05:57:32 +00:00
} )
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 ( )
ret := make ( [ ] * js . Object , 0 , len ( objs ) )
for _ , obj := range objs {
ret = append ( ret , js . MakeFullWrapper ( obj ) )
}
return ret
}
2023-01-01 07:43:25 +00:00
func GenerateRectColliderJs ( wx , wy , w , h , spaceOffsetX , spaceOffsetY float64 , data interface { } , tag string ) * js . Object {
2022-12-24 05:57:32 +00:00
/ *
2022-12-25 06:18:48 +00:00
[ WARNING ] It ' s important to note that we don ' t need "js.MakeFullWrapper" for a call sequence as follows .
` ` `
var space = gopkgs . NewCollisionSpaceJs ( 2048 , 2048 , 8 , 8 ) ;
2023-01-01 07:43:25 +00:00
var a = gopkgs . GenerateRectColliderJs ( 189 , 497 , 48 , 48 , spaceOffsetX , spaceOffsetY , "Player" ) ;
2022-12-25 06:18:48 +00:00
space . Add ( a ) ;
` ` `
The "space" variable doesn ' t need access to the field of "a" in JavaScript level to run "space.Add(...)" method , which is good .
However , the full wrapper access here is used for updating "collider.X/collider.Y" at JavaScript runtime .
2022-12-24 05:57:32 +00:00
* /
2023-01-01 07:43:25 +00:00
topPadding , bottomPadding , leftPadding , rightPadding := SNAP_INTO_PLATFORM_OVERLAP , SNAP_INTO_PLATFORM_OVERLAP , SNAP_INTO_PLATFORM_OVERLAP , SNAP_INTO_PLATFORM_OVERLAP
2022-12-24 14:27:31 +00:00
return js . MakeFullWrapper ( GenerateRectCollider ( wx , wy , w , h , topPadding , bottomPadding , leftPadding , rightPadding , spaceOffsetX , spaceOffsetY , data , tag ) )
2022-12-23 09:31:04 +00:00
}
2022-12-24 11:06:31 +00:00
func GenerateConvexPolygonColliderJs ( unalignedSrc * Polygon2D , spaceOffsetX , spaceOffsetY float64 , data interface { } , tag string ) * js . Object {
2022-12-24 14:27:31 +00:00
return js . MakeFullWrapper ( 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 {
ret [ i ] = js . MakeFullWrapper ( Characters [ speciesId ] )
}
return ret
}
2023-01-01 07:43:25 +00:00
func ApplyInputFrameDownsyncDynamicsOnSingleRenderFrameJs ( inputsBuffer * RingBuffer , currRenderFrame * RoomDownsyncFrame , collisionSys * resolv . Space , collisionSysMap map [ int32 ] * resolv . Object , collisionSpaceOffsetX , collisionSpaceOffsetY float64 , chConfigsOrderedByJoinIndex [ ] * CharacterConfig ) * js . Object {
2022-12-24 05:57:32 +00:00
// We need access to all fields of RoomDownsyncFrame for displaying in frontend
2023-01-01 07:43:25 +00:00
return js . MakeFullWrapper ( ApplyInputFrameDownsyncDynamicsOnSingleRenderFrame ( inputsBuffer , currRenderFrame , collisionSys , collisionSysMap , collisionSpaceOffsetX , collisionSpaceOffsetY , chConfigsOrderedByJoinIndex ) )
2022-12-23 09:31:04 +00:00
}
func main ( ) {
js . Global . Set ( "gopkgs" , map [ string ] interface { } {
2023-01-01 07:43:25 +00:00
"NewVec2DJs" : NewVec2DJs ,
"NewPolygon2DJs" : NewPolygon2DJs ,
"NewBarrierJs" : NewBarrierJs ,
"NewPlayerDownsyncJs" : NewPlayerDownsyncJs ,
"NewMeleeBulletJs" : NewMeleeBulletJs ,
2023-01-10 04:08:15 +00:00
"NewFireballBulletJs" : NewFireballBulletJs ,
2023-01-08 12:34:29 +00:00
"NewNpcPatrolCue" : NewNpcPatrolCue ,
2023-01-01 07:43:25 +00:00
"NewRoomDownsyncFrameJs" : NewRoomDownsyncFrameJs ,
"NewCollisionSpaceJs" : NewCollisionSpaceJs ,
"NewInputFrameDownsync" : NewInputFrameDownsync ,
"NewRingBufferJs" : NewRingBufferJs ,
"GenerateRectColliderJs" : GenerateRectColliderJs ,
"GenerateConvexPolygonColliderJs" : GenerateConvexPolygonColliderJs ,
"GetCollisionSpaceObjsJs" : GetCollisionSpaceObjsJs ,
2022-12-25 06:18:48 +00:00
"WorldToPolygonColliderBLPos" : WorldToPolygonColliderBLPos , // No need to wrap primitive return types
"PolygonColliderBLToWorldPos" : PolygonColliderBLToWorldPos ,
2023-01-01 07:43:25 +00:00
"WorldToVirtualGridPos" : WorldToVirtualGridPos ,
"VirtualGridToWorldPos" : VirtualGridToWorldPos ,
"GetCharacterConfigsOrderedByJoinIndex" : GetCharacterConfigsOrderedByJoinIndex ,
"ApplyInputFrameDownsyncDynamicsOnSingleRenderFrameJs" : ApplyInputFrameDownsyncDynamicsOnSingleRenderFrameJs ,
2023-01-02 15:35:56 +00:00
"ConvertToDelayedInputFrameId" : ConvertToDelayedInputFrameId ,
"ConvertToNoDelayInputFrameId" : ConvertToNoDelayInputFrameId ,
"ConvertToFirstUsedRenderFrameId" : ConvertToFirstUsedRenderFrameId ,
"ConvertToLastUsedRenderFrameId" : ConvertToLastUsedRenderFrameId ,
"ShouldGenerateInputFrameUpsync" : ShouldGenerateInputFrameUpsync ,
2023-01-15 05:11:19 +00:00
"IsMeleeBulletActive" : IsMeleeBulletActive ,
"IsMeleeBulletAlive" : IsMeleeBulletAlive ,
"IsFireballBulletActive" : IsFireballBulletActive ,
"IsFireballBulletAlive" : IsFireballBulletAlive ,
2022-12-23 09:31:04 +00:00
} )
}