Skip to content

Commit

Permalink
Refactor helpers + move player code to demo
Browse files Browse the repository at this point in the history
  • Loading branch information
zpl-zak committed Dec 31, 2018
1 parent 59667a5 commit 5016faa
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 100 deletions.
10 changes: 5 additions & 5 deletions src/core/anim.go
Expand Up @@ -66,20 +66,20 @@ func (o *Object) NewAnim() {
}
}

o.GetAABB = getSpriteAABB
o.GetAABB = GetSpriteAABB

o.Draw = func(o *Object) {
if o.Ase == nil {
return
}

source := getSpriteRectangle(o)
dest := getSpriteOrigin(o)
source := GetSpriteRectangle(o)
dest := GetSpriteOrigin(o)

if DebugMode && o.DebugVisible {
c := getSpriteAABB(o)
c := GetSpriteAABB(o)
rl.DrawRectangleLinesEx(c.ToFloat32(), 1, rl.Blue)
drawTextCentered(o.Name, c.X+c.Width/2, c.Y+c.Height+2, 1, rl.White)
DrawTextCentered(o.Name, c.X+c.Width/2, c.Y+c.Height+2, 1, rl.White)
}

rl.DrawTexturePro(*o.Texture, source, dest, rl.Vector2{}, o.Rotation, SkyColor)
Expand Down
14 changes: 7 additions & 7 deletions src/core/camera.go
Expand Up @@ -140,8 +140,8 @@ func (c *Object) NewCamera() {
}

rl.DrawCircle(int32(o.Position.X), int32(o.Position.Y), 2, rl.White)
drawTextCentered(fmt.Sprintf("%s", o.Name), int32(o.Position.X), int32(o.Position.Y)+5, 10, rl.White)
drawTextCentered(fmt.Sprintf("Mode: %s", mode), int32(o.Position.X), int32(o.Position.Y)+15, 10, rl.White)
DrawTextCentered(fmt.Sprintf("%s", o.Name), int32(o.Position.X), int32(o.Position.Y)+5, 10, rl.White)
DrawTextCentered(fmt.Sprintf("Mode: %s", mode), int32(o.Position.X), int32(o.Position.Y)+15, 10, rl.White)
}
}

Expand All @@ -161,7 +161,7 @@ func finishCamera(c *Object) {
func updateCamera(c *Object, dt float32) {
var dest rl.Vector2

CanSave = bitsSet(CanSave, isSequenceHappening)
CanSave = BitsSet(CanSave, IsSequenceHappening)

if c.Mode == CameraModeFollow {
if c.Follow == nil {
Expand All @@ -170,7 +170,7 @@ func updateCamera(c *Object, dt float32) {
}

if c.Follow == LocalPlayer {
CanSave = bitsClear(CanSave, isSequenceHappening)
CanSave = BitsClear(CanSave, IsSequenceHappening)
}

dest = c.Follow.Position
Expand Down Expand Up @@ -209,16 +209,16 @@ func updateCamera(c *Object, dt float32) {
t = vd
}

c.Position = vector2Lerp(c.Start.Position, dest, c.Progress)
c.Position = Vector2Lerp(c.Start.Position, dest, c.Progress)
c.Progress += t * dt
} else {
c.Position = vector2Lerp(c.Position, dest, t)
c.Position = Vector2Lerp(c.Position, dest, t)
}
} else {
c.Position = dest
}

c.Zoom = scalarLerp(c.Zoom, c.TargetZoom, c.ZoomSpeed*dt)
c.Zoom = ScalarLerp(c.Zoom, c.TargetZoom, c.ZoomSpeed*dt)

c.First = false
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/collision.go
Expand Up @@ -46,7 +46,7 @@ func (o *Object) NewCollision() {
rl.DrawRectangleLines(int32(o.Position.X), int32(o.Position.Y), int32(o.Meta.Width), int32(o.Meta.Height), color)

c := o.GetAABB(o)
drawTextCentered(o.Name, c.X+c.Width/2, c.Y+c.Height+2, 1, rl.White)
DrawTextCentered(o.Name, c.X+c.Width/2, c.Y+c.Height+2, 1, rl.White)
}

o.GetAABB = func(o *Object) rl.RectangleInt32 {
Expand Down
4 changes: 2 additions & 2 deletions src/core/editor.go
Expand Up @@ -21,7 +21,7 @@ import (
)

var (
rootIsCollapsed = false
rootIsCollapsed = true
rootElement = &editorElement{
text: "editor",
isCollapsed: &rootIsCollapsed,
Expand Down Expand Up @@ -62,7 +62,7 @@ func drawEditorElement(element *editorElement, offsetX, offsetY int32) int32 {
color := rl.White
var ext int32 = 10

if element.isCollapsed != nil && isMouseInRectangle(offsetX, offsetY, rl.MeasureText(element.text, 10), 10) {
if element.isCollapsed != nil && IsMouseInRectangle(offsetX, offsetY, rl.MeasureText(element.text, 10), 10) {
color = rl.Red

if rl.IsMouseButtonReleased(rl.MouseLeftButton) {
Expand Down
78 changes: 51 additions & 27 deletions src/core/helpers.go
Expand Up @@ -37,11 +37,19 @@ const (
// Bits represent bitflags
type Bits uint64

func bitsSet(b, flag Bits) Bits { return b | flag }
func bitsClear(b, flag Bits) Bits { return b &^ flag }
func bitsToggle(b, flag Bits) Bits { return b ^ flag }
func bitsHas(b, flag Bits) bool { return b&flag != 0 }
// BitsSet sets a bit
func BitsSet(b, flag Bits) Bits { return b | flag }

// BitsClear clears a bit
func BitsClear(b, flag Bits) Bits { return b &^ flag }

// BitsToggle toggles a bit on/off
func BitsToggle(b, flag Bits) Bits { return b ^ flag }

// BitsHas checks if a bit is set on
func BitsHas(b, flag Bits) bool { return b&flag != 0 }

// RayRectangleInt32ToResolv conv
func rayRectangleInt32ToResolv(rec *resolv.Rectangle, i rl.RectangleInt32) {
*rec = resolv.Rectangle{
BasicShape: resolv.BasicShape{
Expand All @@ -54,40 +62,46 @@ func rayRectangleInt32ToResolv(rec *resolv.Rectangle, i rl.RectangleInt32) {
}
}

func drawTextCentered(text string, posX, posY, fontSize int32, color rl.Color) {
// DrawTextCentered draws a text that is centered
func DrawTextCentered(text string, posX, posY, fontSize int32, color rl.Color) {
if fontSize < 10 {
fontSize = 10
}

rl.DrawText(text, posX-rl.MeasureText(text, fontSize)/2, posY, fontSize, color)
}

func vector2Lerp(v1, v2 rl.Vector2, amount float32) (result rl.Vector2) {
// Vector2Lerp lerps vec2
func Vector2Lerp(v1, v2 rl.Vector2, amount float32) (result rl.Vector2) {
result.X = v1.X + amount*(v2.X-v1.X)
result.Y = v1.Y + amount*(v2.Y-v1.Y)

return result
}

func scalarLerp(v1, v2 float32, amount float32) (result float32) {
// ScalarLerp lerps a scalar value
func ScalarLerp(v1, v2 float32, amount float32) (result float32) {
result = v1 + amount*(v2-v1)

return result
}

func stringToVec2(inp string) rl.Vector2 {
// StringToVec2 conv
func StringToVec2(inp string) rl.Vector2 {
comps := strings.Split(inp, " ")
x, _ := strconv.ParseFloat(comps[0], 32)
y, _ := strconv.ParseFloat(comps[1], 32)

return rl.NewVector2(float32(x), float32(y))
}

func lerpColor(a, b rl.Vector3, t float64) rl.Vector3 {
// LerpColor lerps Color
func LerpColor(a, b rl.Vector3, t float64) rl.Vector3 {
return raymath.Vector3Lerp(a, b, float32(t))
}

func getColorFromHex(hex string) (rl.Vector3, error) {
// GetColorFromHex conv
func GetColorFromHex(hex string) (rl.Vector3, error) {
if hex == "" {
return rl.Vector3{}, fmt.Errorf("hex not specified")
}
Expand All @@ -107,7 +121,8 @@ func getColorFromHex(hex string) (rl.Vector3, error) {
return d, nil
}

func vec3ToColor(a rl.Vector3) rl.Color {
// Vec3ToColor conv
func Vec3ToColor(a rl.Vector3) rl.Color {
return rl.NewColor(
uint8(a.X*255),
uint8(a.Y*255),
Expand All @@ -116,23 +131,26 @@ func vec3ToColor(a rl.Vector3) rl.Color {
)
}

func colorToVec3(a rl.Color) rl.Vector3 {
// ColorToVec3 conv
func ColorToVec3(a rl.Color) rl.Vector3 {
return rl.NewVector3(
float32(a.R)/255.0,
float32(a.G)/255.0,
float32(a.B)/255.0,
)
}

func mixColor(a, b rl.Color) rl.Color {
return vec3ToColor(raymath.Vector3Lerp(
colorToVec3(a),
colorToVec3(b),
// MixColor mixes two colors together
func MixColor(a, b rl.Color) rl.Color {
return Vec3ToColor(raymath.Vector3Lerp(
ColorToVec3(a),
ColorToVec3(b),
0.5,
))
}

func isMouseInRectangle(x, y, x2, y2 int32) bool {
// IsMouseInRectangle checks whether a mouse is inside of a rectangle
func IsMouseInRectangle(x, y, x2, y2 int32) bool {
x2 = x + x2
y2 = y + y2

Expand All @@ -150,7 +168,8 @@ func isMouseInRectangle(x, y, x2, y2 int32) bool {
return false
}

func getSpriteAABB(o *Object) rl.RectangleInt32 {
// GetSpriteAABB retrieves Aseprite boundaries
func GetSpriteAABB(o *Object) rl.RectangleInt32 {
if o.Ase == nil {
return rl.RectangleInt32{
X: int32(o.Position.X),
Expand All @@ -168,24 +187,28 @@ func getSpriteAABB(o *Object) rl.RectangleInt32 {
}
}

func playAnim(p *Object, animName string) {
// PlayAnim plays an animation for a given object
func PlayAnim(p *Object, animName string) {
if p.Ase.GetAnimation(animName) != nil {
p.Ase.Play(animName)
} else {
//log.Println("Animation name:", animName, "not found!")
}
}

func getSpriteRectangle(o *Object) rl.Rectangle {
// GetSpriteRectangle retrieves sprite's bounds
func GetSpriteRectangle(o *Object) rl.Rectangle {
sourceX, sourceY := o.Ase.GetFrameXY()
return rl.NewRectangle(float32(sourceX), float32(sourceY), float32(o.Ase.FrameWidth), float32(o.Ase.FrameHeight))
}

func getSpriteOrigin(o *Object) rl.Rectangle {
// GetSpriteOrigin retrieves sprite's origin
func GetSpriteOrigin(o *Object) rl.Rectangle {
return rl.NewRectangle(o.Position.X-float32(o.Ase.FrameWidth/2), o.Position.Y-float32(o.Ase.FrameHeight/2), float32(o.Ase.FrameWidth), float32(o.Ase.FrameHeight))
}

func isPointWithinRectangle(p rl.Vector2, r rl.Rectangle) bool {
// IsPointWithinRectangle checks whether a point is within a rectangle
func IsPointWithinRectangle(p rl.Vector2, r rl.Rectangle) bool {
if p.X > r.X && p.X < (r.X+r.Width) &&
p.Y > r.Y && p.Y < (r.Y+r.Height) {
return true
Expand All @@ -200,8 +223,8 @@ func GetColorFromProperty(o *tiled.Object, name string) rl.Color {
var color rl.Color

if colorHex != "" {
colorVec, _ := getColorFromHex(colorHex)
color = vec3ToColor(colorVec)
colorVec, _ := GetColorFromHex(colorHex)
color = Vec3ToColor(colorVec)
} else {
color = rl.Blank
}
Expand All @@ -215,7 +238,7 @@ func GetVector2FromProperty(o *tiled.Object, name string) rl.Vector2 {
var vec rl.Vector2

if txtVec != "" {
vec = stringToVec2(txtVec)
vec = StringToVec2(txtVec)
}

return vec
Expand All @@ -236,7 +259,8 @@ func GetFloatFromProperty(o *tiled.Object, name string) float32 {
return flt
}

func isPointWithinFrustum(p rl.Vector2) bool {
// IsPointWithinFrustum checks whether a point is within camera's frustum
func IsPointWithinFrustum(p rl.Vector2) bool {
if MainCamera == nil {
return false
}
Expand All @@ -253,5 +277,5 @@ func isPointWithinFrustum(p rl.Vector2) bool {
Height: float32(system.ScreenHeight)/MainCamera.Zoom + FrustumSafeMargin*2,
}

return isPointWithinRectangle(p, cam)
return IsPointWithinRectangle(p, cam)
}
2 changes: 1 addition & 1 deletion src/core/map.go
Expand Up @@ -414,7 +414,7 @@ func (m *Map) DrawTilemap(renderOverlays bool) {

tilePos := rl.NewVector2(tileWorldX+tileW/2, tileWorldY+tileH/2)

if !isPointWithinFrustum(tilePos) && cullingEnabled {
if !IsPointWithinFrustum(tilePos) && cullingEnabled {
continue
}

Expand Down
19 changes: 14 additions & 5 deletions src/core/savegame.go
Expand Up @@ -33,11 +33,20 @@ var (
)

const (
isInMenu Bits = 1 << iota
isSequenceHappening
isPlayerDead
isInDialogue
isInChallenge
// IsInMenu is player in menu
IsInMenu Bits = 1 << iota

// IsSequenceHappening is a scripted sequence happening
IsSequenceHappening

// IsPlayerDead is it game over yet
IsPlayerDead

// IsInDialogue is player currently in an active dialogue
IsInDialogue

// IsInChallenge is player in a danger zone which disallows saving the game
IsInChallenge
)

// SaveSystem manages game save states
Expand Down
6 changes: 3 additions & 3 deletions src/core/talk.go
Expand Up @@ -73,7 +73,7 @@ func (o *Object) NewTalk() {
return
}

CanSave = bitsSet(CanSave, isInDialogue)
CanSave = BitsSet(CanSave, IsInDialogue)

if o.LastTrigger == 0 {
o.LastTrigger = rl.GetTime()
Expand Down Expand Up @@ -119,7 +119,7 @@ func (o *Object) NewTalk() {

if o.currentText == nil {
o.Started = false
CanSave = bitsClear(CanSave, isInDialogue)
CanSave = BitsClear(CanSave, IsInDialogue)
o.WasExecuted = true
}

Expand Down Expand Up @@ -212,7 +212,7 @@ func (o *Object) NewTalk() {
rl.White,
)

if isMouseInRectangle(chsX, ypos, 200, 15) {
if IsMouseInRectangle(chsX, ypos, 200, 15) {
if rl.IsMouseButtonDown(rl.MouseLeftButton) {
rl.DrawRectangleLines(chsX, ypos, 200, 15, rl.Pink)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/core/target.go
Expand Up @@ -28,6 +28,6 @@ func (o *Object) NewTarget() {
}

rl.DrawCircle(int32(o.Position.X), int32(o.Position.Y), 5, rl.White)
drawTextCentered(o.Name, int32(o.Position.X), int32(o.Position.Y)+5, 10, rl.White)
DrawTextCentered(o.Name, int32(o.Position.X), int32(o.Position.Y)+5, 10, rl.White)
}
}
4 changes: 2 additions & 2 deletions src/core/tile.go
Expand Up @@ -98,7 +98,7 @@ func (o *Object) NewTile() {
rl.DrawCircle(int32(e.X), int32(e.Y), 2, rl.Red)
rl.DrawLineEx(b, e, 1, rl.Yellow)
}
drawTextCentered(o.Name, c.X+c.Width/2, c.Y+c.Height+2, 1, rl.White)
DrawTextCentered(o.Name, c.X+c.Width/2, c.Y+c.Height+2, 1, rl.White)
}

var rot float32
Expand All @@ -121,7 +121,7 @@ func (o *Object) NewTile() {
if o.TintColor == rl.Blank {
tint = SkyColor
} else {
tint = mixColor(o.TintColor, SkyColor)
tint = MixColor(o.TintColor, SkyColor)
}

if o.Fullbright {
Expand Down

0 comments on commit 5016faa

Please sign in to comment.