Created
December 25, 2019 18:56
-
-
Save steelx/7aac3c87e1eb3dfe7599cca170f261c9 to your computer and use it in GitHub Desktop.
Go inheritance with Interface (Entity) + pixelgl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| // Bounds - A bounding (rectangle) box with a x,y origin and width and height | |
| type Bounds struct { | |
| X float64 | |
| Y float64 | |
| Width float64 | |
| Height float64 | |
| entity Entity | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //------------------------------------------------------------- | |
| // Part of the world. Handles its part of world pixels. | |
| //============================================================= | |
| package main | |
| import ( | |
| "github.com/faiface/pixel" | |
| "github.com/faiface/pixel/imdraw" | |
| _ "github.com/faiface/pixel/pixelgl" | |
| ) | |
| //============================================================= | |
| // Chunk Structure | |
| // exmaple: | |
| // c := &chunk{} | |
| // c.create(float64(x), float64(y)) | |
| //============================================================= | |
| type chunk struct { | |
| dirty bool | |
| model *imdraw.IMDraw | |
| bounds Bounds | |
| } | |
| //============================================================= | |
| // Support the Entity interface | |
| //============================================================= | |
| func (c *chunk) hit(x, y float64) bool { | |
| return false | |
| } | |
| func (c *chunk) explode() { | |
| } | |
| func (c *chunk) getMass() float64 { | |
| return 1.0 | |
| } | |
| func (c *chunk) getType() entityType { | |
| return entityChunk | |
| } | |
| func (c *chunk) draw(dt float64) { | |
| if c.dirty { | |
| c.build() | |
| } | |
| c.model.Draw(global.gWin)//global.gWin *pixelgl.Window | |
| } | |
| //============================================================= | |
| // Create a new chunk | |
| //============================================================= | |
| func (c *chunk) create(x, y float64) { | |
| c.model = imdraw.New(nil) | |
| c.dirty = true | |
| c.bounds = Bounds{ | |
| X: x, | |
| Y: y, | |
| Width: wPixelsPerChunk, | |
| Height: wPixelsPerChunk, | |
| entity: Entity(c), | |
| } | |
| } | |
| //============================================================= | |
| // Rebuild/Build the chunk. | |
| //============================================================= | |
| func (c *chunk) build() { | |
| model := imdraw.New(nil) | |
| for x := c.bounds.X; x < c.bounds.X+c.bounds.Width; x++ { | |
| for y := c.bounds.Y; y < c.bounds.Y+c.bounds.Height; y++ { | |
| p := global.gWorld.pixels[int(float64(global.gWorld.width)*x+y)] | |
| if p == 0 { | |
| continue | |
| } | |
| model.Color = pixel.RGB( | |
| float64(p>>24&0xFF)/255.0, | |
| float64(p>>16&0xFF)/255.0, | |
| float64(p>>8&0xFF)/255.0, | |
| ).Mul(pixel.Alpha(float64(p&0xFF) / 255.0)) | |
| model.Push( | |
| pixel.V(float64(x*wPixelSize), float64(y*wPixelSize)), | |
| pixel.V(float64(x*wPixelSize+wPixelSize), float64(y*wPixelSize+wPixelSize)), | |
| ) | |
| model.Rectangle(0) | |
| } | |
| } | |
| c.model = model | |
| c.dirty = false | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| const ( | |
| wPixelsPerChunk = 64 | |
| wPixelSize = 1 | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| type Entity interface { | |
| hit(x, y float64) bool | |
| explode() | |
| getMass() float64 | |
| getType() entityType //iota | |
| draw(dt float64) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment