summaryrefslogtreecommitdiff
path: root/player.go
blob: 0b313ed4e2b4070549b06965b79b67f8d90c3d34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main

import (
    "github.com/hajimehoshi/ebiten/v2"
)

const (
    gravity = 0.01
    friction = 0.6
)

type GameObject struct {
    x, y float32
    vx, vy float32
    image *ebiten.Image
    onGround bool
}


type Player struct {
    GameObject
}

func (o * GameObject) Update(tilemap Tilemap) {
    o.vy += gravity

    o.x += o.vx
    if tilemap.CollideObject(o) {
        o.x -= o.vx
        o.vx = 0
    }

    o.y += o.vy
    if (tilemap.CollideObject(o)) {

        o.onGround = true;
        o.vx *= friction


        o.y -= o.vy
        o.vy = 0
    } else {
        o.onGround = false;
    }
}

func (o * GameObject) Draw(screen *ebiten.Image, tilemap Tilemap) {
    op := &ebiten.DrawImageOptions{}
    op.GeoM.Translate(float64(o.x * float32(tilemap.tileSize)), float64(o.y * float32(tilemap.tileSize)))
    screen.DrawImage(o.image, op)
}