Old hobby attempt at a 3D voxel engine in Go/OpenGL.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

block.go 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package main
  2. type Block interface {
  3. New() Block
  4. Name() string
  5. GetModel(r *Render) Model
  6. GetBoundingBox() BoundingBox
  7. IsSideSolid(d Direction) bool
  8. }
  9. type BlockRegistry struct {
  10. idBlock []Block
  11. nameBlock map[string]Block
  12. nameId map[string]int
  13. maxId int
  14. }
  15. type BlockSimple struct {
  16. name string
  17. textures [6]string
  18. models map[*Render]Model
  19. }
  20. func NewBlockRegistry() BlockRegistry {
  21. return BlockRegistry{
  22. idBlock: make([]Block, 256, 256),
  23. nameId: make(map[string]int, 256),
  24. nameBlock: make(map[string]Block, 256),
  25. maxId: 1,
  26. }
  27. }
  28. func (b *BlockRegistry) ByName(s string) Block {
  29. return b.nameBlock[s]
  30. }
  31. func (b *BlockRegistry) ByID(i int) Block {
  32. return b.idBlock[i]
  33. }
  34. func (b *BlockRegistry) GetID(bb Block) int {
  35. return b.nameId[bb.Name()]
  36. }
  37. func (b *BlockRegistry) allocID() int {
  38. b.maxId++
  39. for b.idBlock[b.maxId] != nil {
  40. b.maxId++
  41. }
  42. return b.maxId - 1
  43. }
  44. func (b *BlockRegistry) Register(bb Block) {
  45. id := b.allocID()
  46. b.nameBlock[bb.Name()] = bb
  47. b.nameId[bb.Name()] = id
  48. b.idBlock[id] = bb
  49. }
  50. func (b *BlockSimple) Name() string {
  51. return b.name
  52. }
  53. func (b *BlockSimple) GetModel(r *Render) Model {
  54. if b.models == nil {
  55. b.models = make(map[*Render]Model, 1)
  56. }
  57. if v, ok := b.models[r]; ok {
  58. return v
  59. }
  60. b.models[r] = NewFullCubeModel([6]Texture{
  61. r.textures[b.textures[0]],
  62. r.textures[b.textures[1]],
  63. r.textures[b.textures[2]],
  64. r.textures[b.textures[3]],
  65. r.textures[b.textures[4]],
  66. r.textures[b.textures[5]],
  67. })
  68. return b.models[r]
  69. }
  70. func (b *BlockSimple) GetBoundingBox() BoundingBox {
  71. return BoundingBox{Vec3{0,0,0}, Vec3{1,1,1}}
  72. }
  73. func (b *BlockSimple) IsSideSolid(d Direction) bool {
  74. return true
  75. }
  76. func (b *BlockSimple) New() Block {
  77. return b
  78. }