Archived
1
0
Fork 0
This repository has been archived on 2026-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
Danmaku/server/ffi/ffi.go

109 lines
2.1 KiB
Go

package ffi
/*
#cgo CFLAGS: -I ${SRCDIR}/lib
#cgo LDFLAGS: -L ${SRCDIR}/lib -l ffi_wrapper
#include "ffi-wrapper.h"
#include <stdlib.h>
*/
import "C"
// Circles
type Circle struct {
cptr *C.Circle
}
func NewCircle(x float64, y float64, rad float64) *Circle {
return &Circle{
cptr: C.new_circle(C.double(x), C.double(y), C.double(rad)),
}
}
func (c *Circle) UpdatePos(x float64, y float64) {
c.cptr.x = C.double(x)
c.cptr.y = C.double(y)
}
func DestroyCircle(circle *Circle) {
if circle.cptr == nil {
return
}
C.destroy_circle(circle.cptr)
}
func (c *Circle) GetPos() (float64, float64) {
return float64(c.cptr.x), float64(c.cptr.y)
}
// Bullets
type Bullet struct {
cptr *C.Bullet
DeletionTick int64
}
// Values for selecting bullet paths
const (
BULLET_LINEAR = 0
ACTIVE_BULLET = -1
)
func NewLinearBullet(tick int64, spawnX float64, spawnY float64, radius float64, velX float64, velY float64) *Bullet {
return &Bullet{
cptr: C.new_bullet(
C.uint8_t(0),
C.int64_t(tick),
C.double(spawnX),
C.double(spawnY),
C.double(radius),
C.double(velX),
C.double(velY),
),
DeletionTick: ACTIVE_BULLET,
}
}
func DestroyBullet(bullet *Bullet) {
if bullet.cptr == nil {
return
}
C.destroy_bullet(bullet.cptr)
}
func (b *Bullet) BeyondKillBoundary(tick int64) bool {
return bool(C.bullet_beyond_kill_boundary(b.cptr, C.int64_t(tick)))
}
func (b *Bullet) CollidesWith(circle *Circle, tick int64) bool {
return bool(C.bullet_collides_with(b.cptr, C.int64_t(tick), circle.cptr))
}
func (b *Bullet) GetPos(tick int64) (float64, float64) {
var x, y C.double
C.bullet_get_current_pos(b.cptr, C.int64_t(tick), &x, &y)
return float64(x), float64(y)
}
func (b *Bullet) GetType() int {
return int(b.cptr.class_)
}
func (b *Bullet) GetRadius() float64 {
return float64(b.cptr.radius)
}
func (b *Bullet) Serialize(tick int64) map[string]any {
x, y := b.GetPos(tick)
return map[string]any{
"class": b.GetType(),
"tick": tick,
"x": x,
"y": y,
"radius": b.GetRadius(),
"vel_x": float64(b.cptr.parameters[0]),
"vel_y": float64(b.cptr.parameters[1]),
}
}