diff --git a/gameobjects/player.go b/gameobjects/player.go index 16b3733..c906b7d 100644 --- a/gameobjects/player.go +++ b/gameobjects/player.go @@ -17,7 +17,7 @@ type Player struct { } func NewPlayer(win *pixelgl.Window, spriteSheet pixel.Picture) (*Player){ - var p *Player + var p Player p.X = win.Bounds().Max.X/2 p.Y = win.Bounds().Max.Y/2 p.PlayerVec = pixel.Vec{ @@ -29,45 +29,38 @@ func NewPlayer(win *pixelgl.Window, spriteSheet pixel.Picture) (*Player){ p.sprFrames = createSpriteFrames(spriteSheet) p.sprite = pixel.NewSprite(spriteSheet, p.sprFrames[p.sprFrame]) p.speed = 10.0 - return p + return &p } func (p *Player) Update(win *pixelgl.Window, dt int64) { - + var vecX, vecY float64 = 0, 0 speed := p.speed if win.Pressed(pixelgl.KeyA) { //left p.sprFrame = 6 - p.X -= speed + vecX -= speed } if win.Pressed(pixelgl.KeyD) { //Right p.sprFrame = 3 - p.X += speed + vecX += speed } if win.Pressed(pixelgl.KeyS) { //Down p.sprFrame = 9 - p.Y -= speed + vecY -= speed } if win.Pressed(pixelgl.KeyW) { //up p.sprFrame = 0 - p.Y += speed + vecY += speed } p.sprite.Set(p.spriteSheet, p.sprFrames[p.sprFrame]) - //p.PlayerVec.X += vecX - //p.PlayerVec.Y += vecY - - p.PlayerVec = pixel.Vec { - X: p.X, - Y: p.Y, - } - //fmt.Println(p.PlayerVec.X,p.PlayerVec.Y) + p.PlayerVec.X += vecX + p.PlayerVec.Y += vecY } func (p *Player) Render(win *pixelgl.Window) { //include batch later - p.sprite.Draw(win, pixel.IM.Scaled(pixel.ZV, 4).Moved(p.PlayerVec)) } diff --git a/main.go b/main.go index cc07f16..6714652 100644 --- a/main.go +++ b/main.go @@ -53,23 +53,6 @@ func run() { bg := pixel.NewSprite(background, background.Bounds()) //batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet) - - /* setting-character-sprite - - basically changed it around so the 12 frames of the character with yellow hair - are added to spritesFrames. - - */ - - var spritesFrames []pixel.Rect - var frameWidth, frameHeight float64 = 96, 96 - for y := spritesheet.Bounds().Min.Y; y < frameHeight*4; y += 96 { - for x := spritesheet.Bounds().Min.X; x < frameWidth*3; x += 96 { - spritesFrames = append(spritesFrames, pixel.R(x, y, x+96, y+96)) // (x, y, width, height) of frame - } - } - //Sprite := pixel.NewSprite(spritesheet, spritesFrames[9]) - var ( camPos = pixel.ZV camZoom = 0.3 @@ -80,16 +63,6 @@ func run() { frames = 0 second = time.Tick(time.Second) ) - //playerX := win.Bounds().Max.X / 2 - //playerY := win.Bounds().Max.Y / 2 - /* - var ( - playerXY = pixel.Vec{ - playerX, - playerY, - } - )*/ - player := gameobjects.NewPlayer(win, spritesheet) var fps int64 = 60 @@ -98,9 +71,6 @@ func run() { var now int64 var dt int64 - //frameCounter := 0 - //sprFrame := 0 - // Game Loop for !win.Closed() { now = time.Now().UnixNano() @@ -112,56 +82,10 @@ func run() { // *** Update begins *** // - /* - playerXY = pixel.Vec{ - playerX, - playerY, - }*/ cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos)) win.SetMatrix(cam) - /* - This is where Sprite and bg was originally. Creating them over and over inside - the main loop is not very good resource management. When i added my own dt, the game was - only going about 22 fps, when it should've been going 60. I moved them - out of the loop and created them up top. It fixed it and now runs faster. - - the tutorial only has it in the loop with the exception that it creates it only - if you click the button to make a tree. It only created it once and added it. - */ - - //mouse := cam.Unproject(win.MousePosition()) - /* - if frames%10==0 { - frameCounter += 1 - } - if frameCounter >= 3 { - frameCounter = 0 - } - speed := 10.0 - if win.Pressed(pixelgl.KeyA) { //left - sprFrame = 6 - playerX -= speed - //Sprite.Set(spritesheet, spritesFrames[sprFrame+frameCounter]) - } - if win.Pressed(pixelgl.KeyD) { //Right - sprFrame = 3 - playerX += speed - //Sprite.Set(spritesheet, spritesFrames[3]) - } - if win.Pressed(pixelgl.KeyS) { //Down - sprFrame = 9 - playerY -= speed - //Sprite.Set(spritesheet, spritesFrames[9]) - } - if win.Pressed(pixelgl.KeyW) { //up - sprFrame = 0 - playerY += speed - //Sprite.Set(spritesheet, spritesFrames[0]) - } - Sprite.Set(spritesheet, spritesFrames[sprFrame+frameCounter]) - */ player.Update(win, dt) camPos.X = player.PlayerVec.X @@ -173,7 +97,6 @@ func run() { // *** Render begin *** // win.Clear(colornames.Forestgreen) bg.Draw(win, pixel.IM.Moved(win.Bounds().Center())) - //Sprite.Draw(win, pixel.IM.Scaled(pixel.ZV, 4).Moved(playerXY)) player.Render(win) //batch.Draw(win)