123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- package tile
- import "gitlab.com/beoran/al5go/al"
- import "gitlab.com/beoran/ebsgo/engine/geometry/point"
- const TILE_W = 32
- const TILE_H = 32
- var showSolid = false;
- type Set struct {
- Tiles []Tile
- Sheet * al.Bitmap
- W int
- H int
- TileW int
- TileH int
- FirstGID int
- };
- type Frame struct {
- Index int
- Duration float64
- }
- type Tile struct {
- Tileset * Set
- Index int
- Flags int
- Kind int
-
-
- Anim int
-
-
- Active int
-
- Wait float64
-
- Time float64
-
- Position point.P
-
- Blend int
-
- BlendMask int
-
- Light int
- LightMAsk int
-
- Shadow int
-
- ShadowMask int
-
- Frames []Frame
-
- ActiveFrame int
- };
- func NewSet(sheet * al.Bitmap, tile_w, tile_h, firstgid int) * Set {
- set := &Set{}
- set.Sheet = sheet
- set.TileW = tile_w
- set.TileH = tile_h
- set.FirstGID = firstgid
- set.W = sheet.Width() / set.TileW
- set.H = sheet.Height()/ set.TileH
- size := set.W * set.H
- set.Tiles = make([]Tile, size)
- for i := 0 ; i < size; i ++ {
- set.Tiles[i].Init(set, i)
- }
- return set
- }
- func (tile * Tile) Init(set * Set, index int) {
- tile.Tileset = set
- tile.Index = index
- tile.Wait = 0.25
- tile.Recalculate()
- }
- func (tile Tile) SheetY(set * Set) int {
- return (tile.Active * set.TileW) / (set.W * set.TileH)
- }
- func (tile Tile) SheetX(set * Set) int {
- return (tile.Active * set.TileW) % (set.W)
- }
- func (tile * Tile) Recalculate() {
- if nil == tile.Tileset {
- return
- }
-
- x := float64(tile.SheetX(tile.Tileset))
- y := float64(tile.SheetY(tile.Tileset))
-
- tile.Position = point.New(x, y)
- }
- func (set * Set) Tile(index int) * Tile {
- if nil != set && index >= 0 && index <= len(set.Tiles) {
- return &set.Tiles[index]
- }
- return nil
- }
- const (
- TILE_NONE = 0
- TILE_WALL = iota
- TILE_WATER = iota
- TILE_LEDGE = iota
- TILE_STAIR = iota
- TILE_PUSH = iota
- TILE_NORTH = iota
- TILE_SOUTH = iota
- TILE_EAST = iota
- TILE_WEST = iota
- TILE_UP = iota
- TILE_DOWN = iota
- TILE_ICE = iota
- )
-
-
- var FlagNames map[string]uint = map[string]uint {
- "wall" : TILE_WALL ,
- "water": TILE_WATER ,
- "ledge": TILE_LEDGE ,
- "stair": TILE_STAIR ,
- "push" : TILE_PUSH ,
- "north": TILE_NORTH ,
- "south": TILE_SOUTH ,
- "east" : TILE_EAST ,
- "west" : TILE_WEST ,
- "up" : TILE_UP ,
- "down" : TILE_DOWN ,
- "ice" : TILE_ICE ,
- }
- func (tile * Tile) SetProperty(property string) {
- val, ok := FlagNames[property];
- if (ok) {
- tile.Flags |= (1 << val)
- }
- }
- func (tile * Tile) HasFlag(flag uint) bool {
- return (tile.Flags & (1 << flag)) == (1 << flag)
- }
- func (tile * Tile) IsWall() bool {
- return tile.HasFlag(TILE_WALL)
- }
- func (frame * Frame) Init(index int, duration float64) {
- frame.Index = index
- frame.Duration = duration
- }
- func (tile Tile) Frame(index int) * Frame {
- if nil == tile.Frames{
- return nil
- } else {
- return &tile.Frames[index]
- }
- }
- func (tile Tile) FrameCount() int {
- if nil == tile.Frames {
- return 0
- } else {
- return len(tile.Frames)
- }
- }
- func (tile * Tile) AddAnimationFrame(index int, duration float64) (frame * Frame) {
- if (nil == tile.Frames) {
- tile.Frames = make([]Frame, 1)
- frame = &tile.Frames[0]
- } else {
- frame = &Frame{}
- tile.Frames = append(tile.Frames, *frame)
- }
-
- frame.Init(index, duration)
- return frame
- }
-
- func (tile * Tile) RewindAnimations() {
- tile.Active = tile.Index
- tile.ActiveFrame = 0
-
- tile.Recalculate()
- }
- func (tile * Tile) UpdateAnimation(dt float64) {
- active := 0;
- frame := tile.Frame(tile.ActiveFrame)
- if nil == frame {
- tile.ActiveFrame = 0
- frame = tile.Frame(tile.ActiveFrame)
- if nil == frame {
- return
- }
- }
-
- tile.Time += dt
-
- if tile.Time < frame.Duration {
- return
- }
-
- tile.ActiveFrame++
- if tile.ActiveFrame >= tile.FrameCount() {
- tile.ActiveFrame = 0
- }
-
- frame = tile.Frame(tile.ActiveFrame);
-
- tile.Time = 0.0
- if nil == frame {
- return
- }
-
- active = frame.Index
- aidtile := tile.Tileset.Tile(active);
-
- if nil == aidtile {
- return
- }
-
- tile.Active = active
-
- tile.Recalculate();
- }
- func (tile * Tile) Update(dt float64) {
- if nil != tile.Frames {
- tile.UpdateAnimation(dt)
- }
- }
- func (set * Set) Update(dt float64) {
- if nil == set.Tiles {
- return
- }
-
- for i := 0 ; i < len(set.Tiles); i ++ {
- tile := &set.Tiles[i]
- tile.Update(dt)
- }
- }
- func (tile Tile) Draw(x, y float32, drawflags int) {
- set := tile.Tileset
- sheet := set.Sheet
- dx := float32(x)
- dy := float32(y)
- sx := float32(tile.Position.X)
- sy := float32(tile.Position.Y)
- sw := float32(set.TileW)
- sh := float32(set.TileH)
- sheet.DrawRegion(sx, sy, sw, sh, dx, dy, drawflags);
-
- if showSolid {
- if (tile.Flags & (1<<TILE_WALL)) == (1<<TILE_WALL) {
- dcolor := al.MapRGB(0xee, 0xee, 0x00)
- al.DrawRectangle(dx, dy, dx+sw, dy+sh, dcolor, 2);
- }
- }
- }
- var tileMaskBuffer * al.Bitmap
- func (tile * Tile) DrawMaskedTo(result * al.Bitmap, mask * al.Bitmap, angle float32, mask_flags int) {
-
-
-
- if nil == tileMaskBuffer {
- bmpflags := al.NewBitmapFlags()
- al.SetNewBitmapFlags(al.CONVERT_BITMAP)
- tileMaskBuffer = al.CreateBitmap(TILE_W, TILE_H)
- al.SetNewBitmapFlags(bmpflags)
- }
-
-
- target := al.TargetBitmap()
-
-
- al.SetTargetBitmap(tileMaskBuffer)
- set := tile.Tileset
- sheet := set.Sheet
- dx := float32(0.0)
- dy := float32(0.0)
- sx := float32(tile.Position.X)
- sy := float32(tile.Position.Y)
- sw := float32(set.TileW )
- sh := float32(set.TileH )
-
- al.SetBlender(al.ADD, al.ONE, al.ZERO)
- sheet.DrawRegion(sx, sy, sw, sh, 0, 0, 0);
-
-
- al.SetBlender(al.ADD, al.ZERO, al.ALPHA)
- mask.Draw(0, 0, mask_flags)
-
-
- al.SetBlender(al.ADD, al.ONE, al.INVERSE_ALPHA)
- sx = 0.0
- sy = 0.0
- if (angle != 0.0) {
- sx = float32(set.TileW) / 2.0
- sy = float32(set.TileH) / 2.0
- dx += sx
- dy += sy
- }
-
-
- al.SetTargetBitmap(result)
- tileMaskBuffer.DrawRotated(sx, sy, dx, dy, angle, 0)
-
- al.SetTargetBitmap(target)
- }
- func (set Set) Tile(index int) * Tile {
- if set.Tiles == nil {
- return nil
- }
-
- if (index < 0) || (index > len(set.Tiles)) {
- return nil
- }
-
- return &set.Tiles[index]
- }
|