|
| 1 | +package enginewrap |
| 2 | + |
| 3 | +// This file provides coordinate system adapters for all managers. |
| 4 | +// SPX uses Y-axis pointing up, while Godot uses Y-axis pointing down. |
| 5 | +// All coordinate conversions are handled here by overriding manager methods. |
| 6 | + |
| 7 | +import ( |
| 8 | + . "github.com/goplus/spbase/mathf" |
| 9 | + gdx "github.com/goplus/spx/v2/pkg/gdspx/pkg/engine" |
| 10 | +) |
| 11 | + |
| 12 | +// flipY flips the Y coordinate to convert between SPX and Godot coordinate systems |
| 13 | +func flipY(pos Vec2) Vec2 { |
| 14 | + return Vec2{X: pos.X, Y: -pos.Y} |
| 15 | +} |
| 16 | + |
| 17 | +// ============================================================================ |
| 18 | +// SpriteMgr Coordinate Adapters |
| 19 | +// ============================================================================ |
| 20 | + |
| 21 | +func (pself *SpriteMgrImpl) SetPosition(obj gdx.Object, pos Vec2) { |
| 22 | + pself.spriteMgrImpl.SetPosition(obj, flipY(pos)) |
| 23 | +} |
| 24 | + |
| 25 | +func (pself *SpriteMgrImpl) GetPosition(obj gdx.Object) Vec2 { |
| 26 | + return flipY(pself.spriteMgrImpl.GetPosition(obj)) |
| 27 | +} |
| 28 | + |
| 29 | +func (pself *SpriteMgrImpl) SetChildPosition(obj gdx.Object, path string, pos Vec2) { |
| 30 | + pself.spriteMgrImpl.SetChildPosition(obj, path, flipY(pos)) |
| 31 | +} |
| 32 | + |
| 33 | +func (pself *SpriteMgrImpl) GetChildPosition(obj gdx.Object, path string) Vec2 { |
| 34 | + return flipY(pself.spriteMgrImpl.GetChildPosition(obj, path)) |
| 35 | +} |
| 36 | + |
| 37 | +func (pself *SpriteMgrImpl) SetVelocity(obj gdx.Object, velocity Vec2) { |
| 38 | + pself.spriteMgrImpl.SetVelocity(obj, flipY(velocity)) |
| 39 | +} |
| 40 | + |
| 41 | +func (pself *SpriteMgrImpl) GetVelocity(obj gdx.Object) Vec2 { |
| 42 | + return flipY(pself.spriteMgrImpl.GetVelocity(obj)) |
| 43 | +} |
| 44 | + |
| 45 | +func (pself *SpriteMgrImpl) SetPivot(obj gdx.Object, pivot Vec2) { |
| 46 | + pself.spriteMgrImpl.SetPivot(obj, flipY(pivot)) |
| 47 | +} |
| 48 | + |
| 49 | +func (pself *SpriteMgrImpl) GetPivot(obj gdx.Object) Vec2 { |
| 50 | + return flipY(pself.spriteMgrImpl.GetPivot(obj)) |
| 51 | +} |
| 52 | + |
| 53 | +func (pself *SpriteMgrImpl) CheckCollisionWithPoint(obj gdx.Object, point Vec2, isTrigger bool) bool { |
| 54 | + return pself.spriteMgrImpl.CheckCollisionWithPoint(obj, flipY(point), isTrigger) |
| 55 | +} |
| 56 | + |
| 57 | +// extra wrap functions |
| 58 | +func (pself *SpriteMgrImpl) GetLastMotion(obj gdx.Object) Vec2 { |
| 59 | + return flipY(pself.spriteMgrImpl.GetLastMotion(obj)) |
| 60 | +} |
| 61 | + |
| 62 | +func (pself *SpriteMgrImpl) GetPositionDelta(obj gdx.Object) Vec2 { |
| 63 | + return flipY(pself.spriteMgrImpl.GetPositionDelta(obj)) |
| 64 | +} |
| 65 | + |
| 66 | +func (pself *SpriteMgrImpl) GetFloorNormal(obj gdx.Object) Vec2 { |
| 67 | + return flipY(pself.spriteMgrImpl.GetFloorNormal(obj)) |
| 68 | +} |
| 69 | + |
| 70 | +func (pself *SpriteMgrImpl) GetWallNormal(obj gdx.Object) Vec2 { |
| 71 | + return flipY(pself.spriteMgrImpl.GetWallNormal(obj)) |
| 72 | +} |
| 73 | + |
| 74 | +func (pself *SpriteMgrImpl) GetRealVelocity(obj gdx.Object) Vec2 { |
| 75 | + return flipY(pself.spriteMgrImpl.GetRealVelocity(obj)) |
| 76 | +} |
| 77 | + |
| 78 | +func (pself *SpriteMgrImpl) SetAnimOffset(obj gdx.Object, offset Vec2) { |
| 79 | + pself.spriteMgrImpl.SetAnimOffset(obj, flipY(offset)) |
| 80 | +} |
| 81 | + |
| 82 | +func (pself *SpriteMgrImpl) GetAnimOffset(obj gdx.Object) Vec2 { |
| 83 | + return flipY(pself.spriteMgrImpl.GetAnimOffset(obj)) |
| 84 | +} |
| 85 | + |
| 86 | +// ============================================================================ |
| 87 | +// SceneMgr Coordinate Adapters |
| 88 | +// ============================================================================ |
| 89 | + |
| 90 | +func (pself *SceneMgrImpl) CreateRenderSprite(texturePath string, pos Vec2, degree float64, scale Vec2, zindex int64, pivot Vec2) gdx.Object { |
| 91 | + return pself.sceneMgrImpl.CreateRenderSprite(texturePath, flipY(pos), degree, scale, zindex, flipY(pivot)) |
| 92 | +} |
| 93 | + |
| 94 | +func (pself *SceneMgrImpl) CreateStaticSprite(texturePath string, pos Vec2, degree float64, scale Vec2, zindex int64, pivot Vec2, colliderType int64, colliderPivot Vec2, colliderParams gdx.Array) gdx.Object { |
| 95 | + return pself.sceneMgrImpl.CreateStaticSprite(texturePath, flipY(pos), degree, scale, zindex, flipY(pivot), colliderType, flipY(colliderPivot), colliderParams) |
| 96 | +} |
| 97 | + |
| 98 | +// ============================================================================ |
| 99 | +// PhysicMgr Coordinate Adapters |
| 100 | +// ============================================================================ |
| 101 | + |
| 102 | +func (pself *PhysicMgrImpl) Raycast(from Vec2, to Vec2, collisionMask int64) gdx.Object { |
| 103 | + return pself.physicMgrImpl.Raycast(flipY(from), flipY(to), collisionMask) |
| 104 | +} |
| 105 | + |
| 106 | +// RaycastWithDetails returns array: [collide, sprite_gid, pos.x, pos.y, normal.x, normal.y] |
| 107 | +// This conversion should be handled at upper layer due to gdx.Array access limitations |
| 108 | +func (pself *PhysicMgrImpl) RaycastWithDetails(from Vec2, to Vec2, ignoreSprites gdx.Array, collisionMask int64, collideWithAreas bool, collideWithBodies bool) gdx.Array { |
| 109 | + return pself.physicMgrImpl.RaycastWithDetails(flipY(from), flipY(to), ignoreSprites, collisionMask, collideWithAreas, collideWithBodies) |
| 110 | +} |
| 111 | + |
| 112 | +func (pself *PhysicMgrImpl) CheckCollisionRect(pos Vec2, size Vec2, collisionMask int64) gdx.Array { |
| 113 | + return pself.physicMgrImpl.CheckCollisionRect(flipY(pos), size, collisionMask) |
| 114 | +} |
| 115 | + |
| 116 | +func (pself *PhysicMgrImpl) CheckCollisionCircle(pos Vec2, radius float64, collisionMask int64) gdx.Array { |
| 117 | + return pself.physicMgrImpl.CheckCollisionCircle(flipY(pos), radius, collisionMask) |
| 118 | +} |
| 119 | + |
| 120 | +// ============================================================================ |
| 121 | +// InputMgr Coordinate Adapters |
| 122 | +// ============================================================================ |
| 123 | + |
| 124 | +func (pself *InputMgrImpl) GetMousePos() Vec2 { |
| 125 | + return flipY(pself.inputMgrImpl.GetMousePos()) |
| 126 | +} |
| 127 | + |
| 128 | +// ============================================================================ |
| 129 | +// DebugMgr Coordinate Adapters |
| 130 | +// ============================================================================ |
| 131 | + |
| 132 | +func (pself *DebugMgrImpl) DebugDrawCircle(pos Vec2, radius float64, color Color) { |
| 133 | + pself.debugMgrImpl.DebugDrawCircle(flipY(pos), radius, color) |
| 134 | +} |
| 135 | + |
| 136 | +func (pself *DebugMgrImpl) DebugDrawRect(pos Vec2, size Vec2, color Color) { |
| 137 | + pself.debugMgrImpl.DebugDrawRect(flipY(pos), size, color) |
| 138 | +} |
| 139 | + |
| 140 | +func (pself *DebugMgrImpl) DebugDrawLine(from Vec2, to Vec2, color Color) { |
| 141 | + pself.debugMgrImpl.DebugDrawLine(flipY(from), flipY(to), color) |
| 142 | +} |
| 143 | + |
| 144 | +// ============================================================================ |
| 145 | +// TilemapMgr Coordinate Adapters |
| 146 | +// ============================================================================ |
| 147 | + |
| 148 | +func (pself *TilemapMgrImpl) PlaceTile(pos Vec2, texturePath string) { |
| 149 | + pself.tilemapMgrImpl.PlaceTile(flipY(pos), texturePath) |
| 150 | +} |
| 151 | + |
| 152 | +func (pself *TilemapMgrImpl) PlaceTileWithLayer(pos Vec2, texturePath string, layerIndex int64) { |
| 153 | + pself.tilemapMgrImpl.PlaceTileWithLayer(flipY(pos), texturePath, layerIndex) |
| 154 | +} |
| 155 | + |
| 156 | +func (pself *TilemapMgrImpl) EraseTile(pos Vec2) { |
| 157 | + pself.tilemapMgrImpl.EraseTile(flipY(pos)) |
| 158 | +} |
| 159 | + |
| 160 | +func (pself *TilemapMgrImpl) EraseTileWithLayer(pos Vec2, layerIndex int64) { |
| 161 | + pself.tilemapMgrImpl.EraseTileWithLayer(flipY(pos), layerIndex) |
| 162 | +} |
| 163 | + |
| 164 | +func (pself *TilemapMgrImpl) GetTile(pos Vec2) string { |
| 165 | + return pself.tilemapMgrImpl.GetTile(flipY(pos)) |
| 166 | +} |
| 167 | + |
| 168 | +func (pself *TilemapMgrImpl) GetTileWithLayer(pos Vec2, layerIndex int64) string { |
| 169 | + return pself.tilemapMgrImpl.GetTileWithLayer(flipY(pos), layerIndex) |
| 170 | +} |
| 171 | + |
| 172 | +func (pself *TilemapMgrImpl) SetLayerOffset(index int64, offset Vec2) { |
| 173 | + pself.tilemapMgrImpl.SetLayerOffset(index, flipY(offset)) |
| 174 | +} |
| 175 | + |
| 176 | +func (pself *TilemapMgrImpl) GetLayerOffset(index int64) Vec2 { |
| 177 | + return flipY(pself.tilemapMgrImpl.GetLayerOffset(index)) |
| 178 | +} |
| 179 | + |
| 180 | +// ============================================================================ |
| 181 | +// NavigationMgr Coordinate Adapters |
| 182 | +// ============================================================================ |
| 183 | + |
| 184 | +// FindPath returns array: [x0, y0, x1, y1, x2, y2, ...] |
| 185 | +// This conversion should be handled at upper layer due to gdx.Array access limitations |
| 186 | +func (pself *NavigationMgrImpl) FindPath(from Vec2, to Vec2, withJump bool) gdx.Array { |
| 187 | + var arr = pself.navigationMgrImpl.FindPath(flipY(from), flipY(to), withJump) |
| 188 | + result := arr.([]float32) |
| 189 | + if result == nil { |
| 190 | + return nil |
| 191 | + } |
| 192 | + // flipY to convert godot coord to spx coord |
| 193 | + for i := 0; i+1 < len(result); i += 2 { |
| 194 | + result[i+1] = -result[i+1] |
| 195 | + } |
| 196 | + return result |
| 197 | +} |
0 commit comments