func main() { var ( splash *Splash system *twodee.System window *twodee.Window err error ) system, err = twodee.Init() Check(err) defer system.Terminate() window = &twodee.Window{ Width: 800, Height: 600, Title: "TDoS", Fullscreen: false, } system.Open(window) if !DEBUG { splash, err = InitSplash(system, window, 0) Check(err) for splash.Running() { splash.Paint() } splash = nil } state, err := Init(system, window) Check(err) tick := time.Now() state.UpdateViewport(0) for state.Running() { elapsed := time.Since(tick) //fmt.Printf("Elapsed: %v\n", float32(elapsed) / float32(time.Millisecond)) tick = time.Now() ms := Min(float32(elapsed)/float32(time.Millisecond), 50) state.CheckKeys(ms) state.Update(ms) state.UpdateViewport(ms) state.Paint(ms) } if !DEBUG { frame := 1 if state.Victory { frame = 2 } splash, err = InitSplash(system, window, frame) Check(err) for splash.Running() { splash.Paint() } } }
func InitSplash(system *twodee.System, window *twodee.Window, frame int) (splash *Splash, err error) { if system.LoadTexture("splash", "assets/splash-fw.png", twodee.IntNearest, 0); err != nil { return } splash = &Splash{ running: true, window: window, system: system, scene: &twodee.Scene{}, } system.SetKeyCallback(func(k, s int) { threshold := time.Duration(500) * time.Millisecond if time.Now().After(splash.started.Add(threshold)) { splash.running = false } }) fmt.Println(system.Textures) splash.sprite = system.NewSprite("splash", 0, 0, int(window.View.Dx()), int(window.View.Dy()), 0) splash.sprite.SetFrame(frame) splash.scene.AddChild(splash.sprite) splash.started = time.Now() return }
func Init(system *twodee.System, window *twodee.Window) (state *State, err error) { state = &State{} state.creatures = make([]*Creature, 0) state.boundaries = make([]*twodee.Sprite, 0) state.hud = &twodee.Scene{} state.scene = &twodee.Scene{} state.env = &twodee.Env{} state.window = window state.system = system textures := []TexInfo{ TexInfo{"level-textures", "assets/level-textures.png", 16}, TexInfo{"enemy-sm-textures", "assets/enemy-sm-textures-fw.png", 0}, TexInfo{"enemy-textures", "assets/enemy-textures-fw.png", 0}, TexInfo{"font1-textures", "assets/font1-textures.png", 0}, TexInfo{"darwin-textures", "assets/darwin-textures.png", 0}, TexInfo{"powerups-textures", "assets/powerups-textures-fw.png", 0}, } for _, t := range textures { if err = system.LoadTexture(t.Name, t.Path, twodee.IntNearest, t.Width); err != nil { return } } BlockHandler := func(block *twodee.EnvBlock, sprite *twodee.Sprite, x float32, y float32) { state.HandleAddBlock(block, sprite, x, y) } opts := twodee.EnvOpts{ Blocks: []*twodee.EnvBlock{ &twodee.EnvBlock{ Color: color.RGBA{153, 102, 0, 255}, // Dirt Type: FLOOR, FrameIndex: 0, Handler: BlockHandler, }, &twodee.EnvBlock{ Color: color.RGBA{0, 204, 51, 255}, // Green top Type: FLOOR, FrameIndex: 1, Handler: BlockHandler, }, &twodee.EnvBlock{ Color: color.RGBA{51, 102, 0, 255}, // Top left corner Type: FLOOR, FrameIndex: 2, Handler: BlockHandler, }, &twodee.EnvBlock{ Color: color.RGBA{51, 153, 0, 255}, // Top right corner Type: FLOOR, FrameIndex: 3, Handler: BlockHandler, }, &twodee.EnvBlock{ Color: color.RGBA{153, 153, 51, 255}, // Left dirt wall Type: FLOOR, FrameIndex: 4, Handler: BlockHandler, }, &twodee.EnvBlock{ Color: color.RGBA{153, 153, 102, 255}, // Right dirt wall Type: FLOOR, FrameIndex: 5, Handler: BlockHandler, }, &twodee.EnvBlock{ Color: color.RGBA{204, 204, 51, 255}, // Left grass cap Type: FLOOR, FrameIndex: 6, Handler: BlockHandler, }, &twodee.EnvBlock{ Color: color.RGBA{204, 204, 102, 255}, // Right grass cap Type: FLOOR, FrameIndex: 7, Handler: BlockHandler, }, &twodee.EnvBlock{ Color: color.RGBA{153, 153, 153, 255}, // Rock Type: FLOOR, FrameIndex: 8, Handler: BlockHandler, }, &twodee.EnvBlock{ Color: color.RGBA{118, 118, 118, 255}, // Rock left Type: FLOOR, FrameIndex: 9, Handler: BlockHandler, }, &twodee.EnvBlock{ Color: color.RGBA{84, 84, 84, 255}, // Rock right Type: FLOOR, FrameIndex: 10, Handler: BlockHandler, }, &twodee.EnvBlock{ Color: color.RGBA{0, 0, 0, 255}, Type: START, FrameIndex: 1, Handler: BlockHandler, }, &twodee.EnvBlock{ Color: color.RGBA{51, 51, 51, 255}, Type: BADGUY, FrameIndex: -1, Handler: BlockHandler, }, }, TextureName: "level-textures", MapPath: "assets/level-fw.png", BlockWidth: 32, BlockHeight: 32, } if err = state.env.Load(system, opts); err != nil { return } state.system.SetClearColor(102, 204, 255, 255) state.scene.AddChild(state.env) state.system.SetKeyCallback(func(k, s int) { state.HandleKeys(k, s) }) state.screenxmin = float32(-state.env.Width()) + state.window.View.Max.X state.screenxmax = 0 state.screenymin = float32(-state.env.Height()) + state.window.View.Max.Y state.screenymax = 0 // Do this later so that the hud renders on top of things state.scene.AddChild(state.hud) state.livesbar = NewLivesBar(system, 0, 0) state.hud.AddChild(state.livesbar) state.healthbar = NewLivesBar(system, 0, 0) state.healthbar.Availframe = 3 state.healthbar.Emptyframe = 2 state.healthbar.MoveTo(twodee.Pt(0, 24)) state.hud.AddChild(state.healthbar) state.textscore = system.NewText("font1-textures", 0, 0, 2, "") state.hud.AddChild(state.textscore) state.textfps = system.NewText("font1-textures", 0, float32(state.window.View.Max.Y-32), 1, "") state.hud.AddChild(state.textfps) state.hud.SetZ(0.5) state.nextlife = 400 state.SetScore(0) state.ChangeMaxLives(1) state.ChangeLives(1) state.SetMaxHealth(3) state.ChangeHealth(3) state.running = true state.Victory = false return }