summaryrefslogtreecommitdiff
path: root/engine/Components/World.go
blob: 397cbb7db640d2156e6a650efec7c02db773519f (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package components

import (
	coreobjects "github.com/DegustatorPonos/RuinesOfRafdolon/CoreObjects"
	rl "github.com/gen2brain/raylib-go/raylib"
)

// The scene implimetation that represents one playable scene
type World struct {
	Manager coreobjects.SceneManager
	Name string
	Floor []FloorTile
	StaticObjects []coreobjects.GameObject

	Player *Player

	Camera *rl.Camera2D

	// ========== Cache ==========

	// the collection of the colliders that belong to static objects - world, buildings, etc
	staticColliders []*coreobjects.Collider
}

func (base *World) Create(manager coreobjects.SceneManager) {
	base.Manager = manager
	base.Player.Init(manager)
}

func (base *World) Destroy() {
	base.Player.Destroy()
}

func (base *World) Update() {
	base.Player.Update()
	base.Player.SnapCamera(base.Camera)
	base.handleZoom()
}

func (base *World) handleZoom() {
	var zoomSpeed = 50 * rl.GetFrameTime()
	base.Camera.Zoom += zoomSpeed * rl.GetMouseWheelMove()
}

func (base *World) collectStaticColliders() {
	base.staticColliders = make([]*coreobjects.Collider, 0)
}

func (base *World) GetStaticColliders() []*coreobjects.Collider {
	if base.staticColliders == nil {
		base.collectStaticColliders()
	}
	return base.staticColliders
}

func (base *World) Draw() {
	rl.BeginMode2D(*base.Camera)
	defer rl.EndMode2D()
	for _, v := range base.Floor {
		v.Draw()
	}
	for _, v := range base.StaticObjects {
		v.Draw()
	}
	base.Player.Draw()
}

// The single texture drawn at level 0 of the world
type FloorTile struct {
	Position rl.Vector2
	Texture *rl.Texture2D
}

func (base *FloorTile) Draw() {
	rl.DrawTexture(
		*base.Texture,
		int32(base.Position.X), 
		int32(base.Position.Y),
		rl.White,
	)
}