entity

package
v0.0.0-...-ee7ecfe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2019 License: Apache-2.0 Imports: 13 Imported by: 0

README

Entity包介绍(持续开发中)

名词介绍:
Entity:实体,是player,monster,npc甚至是防御塔等游戏实体的基类,内置了aoi,属性,道具
等属性。
Space:空间,本身也是一种Entity,这里定义为Entity的容器,既然叫做空间,自然拥有AOI计算的能力。
AOI:Area Of Interest,即实时关注区域,实质是一种数据结构,用于记录变动的位置和关注信息。
Entity管理:
	1. 实体Entity-ID和实体Entity的映射关系
	2. 网络层和实体Entity的映射关系
	3. 服务service和实体Entity的关系(举个例子:LOL中温泉是个实体,它提供了一种回血和攻击的服务)
	4. 提供注册Entity的能力
Space管理:
	1. 实体Space-ID和实体Space的映射关系
	2. 提供注册Space的能力
AOI管理:
	AOI管理其实被抽象成十字链表算法,维护着Entity进入、离开、移动时的位置和邻居信息,
	它的存在就是实时记录实体Entity的位置信息和实时计算实体Entity的关注区域
路径管理:
	路径管理封装了a*寻路和基于矩形的碰撞检测,目前还没有实现RVO碰撞检测。
	
规划:
A 所有实体的aoi的由服务器控制(aoi由单进程调度,相比于多进程互斥锁的调度方式更好).
关于进程的使用,建议使用封装后的goroutine
space中统一管理着entity的进入,离开,和移动
entity有哪些:
1. player
2. 小兵
3. 防御塔
...
B 技能系统是一套独立的系统,普通攻击也算是一种技能,每种有技能的实体都会绑定一棵AI行为树.

Documentation

Overview

entityid->*Entity

网络对象

space统一维护aoi数据的变化 不可也没有必要并发调用space接口

Index

Constants

View Source
const (
	NIL_SPACE           = 0 // 空场景
	DEFAULT_FIGHT_SPACE = 1 // 默认的战斗场景
)
View Source
const (
	KindPlain    = iota // 平原
	KindRiver           // 河流
	KindMountain        // 山峰
	KindBlocker         // 阻挡
	KindFrom            // 起点
	KindTo              // 终点
	KindPath            // 路径点
)

Variables

View Source
var KindCosts = map[int]float64{
	KindPlain:    1.0,
	KindFrom:     1.0,
	KindTo:       1.0,
	KindRiver:    2.0,
	KindMountain: 3.0,
}

定义消耗

View Source
var KindRunes = map[int]rune{
	KindPlain:    '.',
	KindRiver:    '~',
	KindMountain: 'M',
	KindBlocker:  'X',
	KindFrom:     'F',
	KindTo:       'T',
	KindPath:     '•',
}

用于渲染 FIXME 仅用于测试

View Source
var RuneKinds = map[rune]int{
	'.': KindPlain,
	'~': KindRiver,
	'M': KindMountain,
	'X': KindBlocker,
	'F': KindFrom,
	'T': KindTo,
	'•': KindPath,
}

用于渲染 FIXME 仅用于测试

View Source
var RuneKindsString = map[string]int{
	"-1": KindPlain,
	"29": KindBlocker,
}

这部分跟导出的地图数据结构相关,完全可以自定义

Functions

func RegisterEntity

func RegisterEntity(entity *Entity)

func RegisterSpace

func RegisterSpace(space *Space)

func UnRegisterEntity

func UnRegisterEntity(entity EntityId)

func UnRegisterSpace

func UnRegisterSpace(space SpaceId)

Types

type Coord

type Coord float32

单位

type Entity

type Entity struct {
	Id        EntityId    // id标识
	Desc      *EntityDesc // 特征描述
	I         Ientity     // entity装载器
	Space     *Space      // 属于哪个space
	Destroyed bool        // 销毁标记:true-已销毁
	Client    *GameClient // 网络对象

	Att *EntityAtt // 属性
	// contains filtered or unexported fields
}

func GetEntity

func GetEntity(entity EntityId) *Entity

func NewEntity

func NewEntity(flag int32, name string, useAOI, persistent bool) *Entity

新建一个实体 @params flag:实体类型 name:实体名称 useAOI:是否具有aoi属性 persistent:是否需要持久化

func (*Entity) BindGameClient

func (e *Entity) BindGameClient(clientid int32)

绑定clientid FIXME 绑定适当的client可以让entity具有网络传输能力

func (*Entity) BindIentity

func (e *Entity) BindIentity(ientity Ientity)

绑定Ientity

func (*Entity) Decrease

func (e *Entity) Decrease(key string, value float32) float32

func (*Entity) DistanceTo

func (e *Entity) DistanceTo(other *Entity) Coord

计算两个Entity之间的距离

func (*Entity) EnterSpace

func (e *Entity) EnterSpace(spaceID SpaceId, pos Vector3)

进入space

func (*Entity) Flag

func (e *Entity) Flag() int32

func (*Entity) GetAttr

func (e *Entity) GetAttr(key string) float32

func (*Entity) GetPosition

func (e *Entity) GetPosition() Vector3

获取当前位置

func (*Entity) Increase

func (e *Entity) Increase(key string, value float32) float32

func (*Entity) IsNeighbor

func (e *Entity) IsNeighbor(other *Entity) bool

判断指定Entity是不是邻居

func (*Entity) IsPersistent

func (e *Entity) IsPersistent() bool

func (*Entity) IsUseAOI

func (e *Entity) IsUseAOI() bool

是否具有aoi

func (*Entity) LeaveSpace

func (e *Entity) LeaveSpace()

离开space

func (*Entity) MoveSpace

func (e *Entity) MoveSpace(pos Vector3)

在space中移动

func (*Entity) Neighbors

func (e *Entity) Neighbors() EntitySet

获取邻居列表

func (*Entity) OnCreated

func (e *Entity) OnCreated()

func (*Entity) OnDestroy

func (e *Entity) OnDestroy()

func (*Entity) OnEnterSpace

func (e *Entity) OnEnterSpace()

func (*Entity) OnInit

func (e *Entity) OnInit()

func (*Entity) OnLeaveSpace

func (e *Entity) OnLeaveSpace(space *Space)

func (*Entity) OnMigrateIn

func (e *Entity) OnMigrateIn()

func (*Entity) OnMigrateOut

func (e *Entity) OnMigrateOut()

func (*Entity) OnRestored

func (e *Entity) OnRestored()

func (*Entity) Post

func (e *Entity) Post(users []*Entity, msg interface{})

异步消息

func (*Entity) String

func (e *Entity) String() string

字符串

type EntityAtt

type EntityAtt struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

type EntityDesc

type EntityDesc struct {
	Name       string // 类型名称
	UseAOI     bool   // 是否具有AOI,有-true
	Persistent bool   // 是否持久化,有-true
	Flag       int32  // 类型标识
}

type EntityId

type EntityId string

type EntityManager

type EntityManager struct {
	// contains filtered or unexported fields
}

type EntitySet

type EntitySet map[*Entity]struct{}

func (EntitySet) Add

func (es EntitySet) Add(entity *Entity)

func (EntitySet) Contains

func (es EntitySet) Contains(entity *Entity) bool

func (EntitySet) Del

func (es EntitySet) Del(entity *Entity)

func (EntitySet) String

func (es EntitySet) String() string

type GameClient

type GameClient struct {
	// contains filtered or unexported fields
}

func (*GameClient) GetId

func (game *GameClient) GetId() int32

func (*GameClient) Post

func (game *GameClient) Post(user []*Entity, msg interface{})

type Iaoicalculator

type Iaoicalculator interface {
	Enter(aoi *aoi, pos Vector3)   // 进入(Entity进入Space时调用)
	Leave(aoi *aoi)                // 离开(Entity离开Space时调用
	Move(aoi *aoi, newPos Vector3) // 移动(Entity在Space里移动时调用)
	// 调整关注区域列表(即调整邻居的过程,可能有新邻居,可能有失效的邻居,
	// 那么也就有不同的操作,对新邻居是进入,对失效邻居是离开)
	// @return enter:新邻居 leave:失效的邻居
	Adjust(aoi *aoi) (enter []*aoi, leave []*aoi)
}

aoi计算器

type Ientity

type Ientity interface {
	OnInit()                   // 初始化
	OnCreated()                // 创建
	OnDestroy()                // 销毁
	OnMigrateOut()             // space迁移
	OnMigrateIn()              // space迁移
	OnRestored()               // 恢复
	OnEnterSpace()             // 进入space
	OnLeaveSpace(space *Space) // 离开space
	IsPersistent() bool        // 是否需要数据持久化
	Flag() int32               // 获取标识
}

type Ispace

type Ispace interface {
	OnSpaceInit()                      // space初始化
	OnSpaceCreated()                   // space创建
	OnSpaceDestroy()                   // space销毁
	OnEntityEnterSpace(entity *Entity) // 当任意Entity进入space时
	OnEntityLeaveSpace(entity *Entity) // 当任意Entity离开space时
}

type Pather

type Pather interface {
	// 以自己为中心获取周围的可行走点
	PathNeighbors() []Pather
	// 获取自己到周围任意可行走点to的消耗
	PathNeighborCost(to Pather) float64
	// 计算自己到周围任意可行走点to的路径长度
	PathEstimatedCost(to Pather) float64
}

func Path

func Path(from, to Pather) (path []Pather, distance float64, found bool)

type Space

type Space struct {
	Id   SpaceId // 场景id
	Type int     // space类型
	I    Ispace  // space装载器
	// contains filtered or unexported fields
}

func GetSpace

func GetSpace(space SpaceId) *Space

func NewSpace

func NewSpace(spaceType int, ispace Ispace) *Space

func (*Space) IsNil

func (space *Space) IsNil() bool

检查是不是空场景

func (*Space) OnEntityEnterSpace

func (space *Space) OnEntityEnterSpace(entity *Entity)

func (*Space) OnEntityLeaveSpace

func (space *Space) OnEntityLeaveSpace(entity *Entity)

func (*Space) OnSpaceCreated

func (space *Space) OnSpaceCreated()

func (*Space) OnSpaceDestroy

func (space *Space) OnSpaceDestroy()

func (*Space) OnSpaceInit

func (space *Space) OnSpaceInit()

func (*Space) String

func (space *Space) String() string

type SpaceId

type SpaceId string

type SpaceManager

type SpaceManager struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

type Vector3

type Vector3 struct {
	X    Coord // X轴
	Y    Coord // Y轴
	Z    Coord // Z轴
	VX   Coord // X轴上的速度
	VZ   Coord // Z轴上的速度
	W    Coord // 度宽,for体积
	H    Coord // 高宽,for体积
	TIME int64 // 时间戳:ms
}

位置

func (Vector3) Add

func (p Vector3) Add(o Vector3) Vector3

p+o

func (Vector3) DistanceTo

func (p Vector3) DistanceTo(o Vector3) Coord

计算p、o两个位置间的距离

func (Vector3) Mul

func (p Vector3) Mul(m Coord) Vector3

p*m

func (*Vector3) Normalize

func (p *Vector3) Normalize()

func (Vector3) Normalized

func (p Vector3) Normalized() Vector3

func (Vector3) String

func (p Vector3) String() string

func (Vector3) Sub

func (p Vector3) Sub(o Vector3) Vector3

p-o

type WayPoint

type WayPoint struct {
	Kind int      // 类型
	X, Y int      // 坐标
	Id   EntityId // 实体id
	W    World    // 属主
}

func (*WayPoint) PathEstimatedCost

func (way *WayPoint) PathEstimatedCost(to Pather) float64

func (*WayPoint) PathNeighborCost

func (way *WayPoint) PathNeighborCost(to Pather) float64

func (*WayPoint) PathNeighbors

func (way *WayPoint) PathNeighbors() []Pather

FIXME 忽略体积碰撞

type World

type World map[int]map[int]*WayPoint

路径点构成的世界 map[WayPoint.X]map[WayPoint.Y]*WayPoint

func ParseWorld

func ParseWorld(input string) World

渲染世界 FIXME 仅用于测试

func ParseWorldByCSV

func ParseWorldByCSV(input string) World

渲染世界通过csv文件

func (World) AllOfKind

func (w World) AllOfKind(kind int) []*WayPoint

func (World) FirstOfKind

func (w World) FirstOfKind(kind int) *WayPoint

func (World) From

func (w World) From() *WayPoint

获取起点

func (World) GetWayPoint

func (w World) GetWayPoint(x, y int) *WayPoint

func (World) RenderPath

func (w World) RenderPath(path []Pather) string

渲染路径 FIXME 仅用于测试

func (World) SetWayPoint

func (w World) SetWayPoint(way *WayPoint, x, y int)

func (World) To

func (w World) To() *WayPoint

获取终点

type XZListAOICalculator

type XZListAOICalculator struct {
	// contains filtered or unexported fields
}

func (*XZListAOICalculator) Adjust

func (cal *XZListAOICalculator) Adjust(aoi *aoi) (enter []*aoi, leave []*aoi)

func (*XZListAOICalculator) Enter

func (cal *XZListAOICalculator) Enter(aoi *aoi, pos Vector3)

func (*XZListAOICalculator) Leave

func (cal *XZListAOICalculator) Leave(aoi *aoi)

func (*XZListAOICalculator) Move

func (cal *XZListAOICalculator) Move(aoi *aoi, pos Vector3)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL