Skip to content

Commit

Permalink
Updated to Macaw v0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
rennomarcus committed Sep 22, 2018
1 parent b33f3d2 commit b5c32a1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
41 changes: 22 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,47 +75,50 @@ func initializeEntities(em *entity.Manager, systems []system.Systemer, font *ttf
vel := &math.FPoint{0, 1}

// player
player.AddComponent("position", &entity.PositionComponent{&sdl.Point{20, 20}})
player.AddComponent("collision", &entity.CollisionComponent{CollisionAreas: []sdl.Rect{sdl.Rect{0, 0, 10, 80}}})
player.AddComponent("geometry", &entity.RectangleComponent{
player.AddComponent(&entity.PositionComponent{&sdl.Point{20, 20}})
player.AddComponent(&entity.CollisionComponent{CollisionAreas: []sdl.Rect{sdl.Rect{0, 0, 10, 80}}})
player.AddComponent(&entity.RenderComponent{RenderType: entity.RTGeometry})
player.AddComponent(&entity.RectangleComponent{
Size: &sdl.Point{10, 80},
Color: &sdl.Color{0x66, 0x66, 0x66, 0xFF},
Filled: true,
})

// computer
computer.AddComponent("position", &entity.PositionComponent{&sdl.Point{770, 20}})
computer.AddComponent("physics", &entity.PhysicsComponent{Vel: vel, Acc: acc, FuturePos: &math.FPoint{770, 20}})
computer.AddComponent("collision", &entity.CollisionComponent{CollisionAreas: []sdl.Rect{sdl.Rect{0, 0, 10, 80}}})
computer.AddComponent("geometry", &entity.RectangleComponent{
computer.AddComponent(&entity.PositionComponent{&sdl.Point{770, 20}})
computer.AddComponent(&entity.PhysicsComponent{Vel: vel, Acc: acc, FuturePos: &math.FPoint{770, 20}})
computer.AddComponent(&entity.CollisionComponent{CollisionAreas: []sdl.Rect{sdl.Rect{0, 0, 10, 80}}})
computer.AddComponent(&entity.RenderComponent{RenderType: entity.RTGeometry})
computer.AddComponent(&entity.RectangleComponent{
Size: &sdl.Point{10, 80},
Color: &sdl.Color{0xFF, 0x66, 0x66, 0xFF},
Filled: true,
})

// ball
ball.AddComponent("position", &entity.PositionComponent{&sdl.Point{300, 20}})
ball.AddComponent("physics", &entity.PhysicsComponent{Vel: &math.FPoint{8, 1}, Acc: acc, FuturePos: &math.FPoint{300, 20}})
ball.AddComponent("collision", &entity.CollisionComponent{CollisionAreas: []sdl.Rect{sdl.Rect{0, 0, 10, 10}}})
ball.AddComponent("geometry", &entity.RectangleComponent{
ball.AddComponent(&entity.PositionComponent{&sdl.Point{300, 20}})
ball.AddComponent(&entity.PhysicsComponent{Vel: &math.FPoint{8, 1}, Acc: acc, FuturePos: &math.FPoint{300, 20}})
ball.AddComponent(&entity.CollisionComponent{CollisionAreas: []sdl.Rect{sdl.Rect{0, 0, 10, 10}}})
ball.AddComponent(&entity.RenderComponent{RenderType: entity.RTGeometry})
ball.AddComponent(&entity.RectangleComponent{
Size: &sdl.Point{10, 10},
Color: &sdl.Color{0x00, 0x00, 0x00, 0xFF},
Filled: false,
})

// player score
playerScore.AddComponent("position", &entity.PositionComponent{&sdl.Point{200, 20}})
playerScore.AddComponent("font", &entity.FontComponent{Text: "0", Modified: true, Font: font})
playerScore.AddComponent("render", &entity.RenderComponent{Renderer: render.Renderer})
playerScore.AddComponent(&entity.PositionComponent{&sdl.Point{200, 20}})
playerScore.AddComponent(&entity.FontComponent{Text: "0", Modified: true, Font: font})
playerScore.AddComponent(&entity.RenderComponent{RenderType: entity.RTFont})

// computer score
computerScore.AddComponent("position", &entity.PositionComponent{&sdl.Point{500, 20}})
computerScore.AddComponent("font", &entity.FontComponent{Text: "0", Modified: true, Font: font})
computerScore.AddComponent("render", &entity.RenderComponent{Renderer: render.Renderer})
computerScore.AddComponent(&entity.PositionComponent{&sdl.Point{500, 20}})
computerScore.AddComponent(&entity.FontComponent{Text: "0", Modified: true, Font: font})
computerScore.AddComponent(&entity.RenderComponent{RenderType: entity.RTFont})

// camera
camera.AddComponent("position", &entity.PositionComponent{&sdl.Point{0, 0}})
camera.AddComponent("camera", &entity.CameraComponent{
camera.AddComponent(&entity.PositionComponent{&sdl.Point{0, 0}})
camera.AddComponent(&entity.CameraComponent{
ViewportSize: sdl.Point{800, 600},
WorldSize: sdl.Point{800, 600},
})
Expand Down
4 changes: 2 additions & 2 deletions psystem/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ func (a *AiSystem) Update() {
computer := a.EntityManager.Get(1)
ball := a.EntityManager.Get(2)

component, _ := ball.GetComponent("physics")
component := ball.GetComponent(&entity.PhysicsComponent{})
ballPhysics := component.(*entity.PhysicsComponent)

component, _ = computer.GetComponent("physics")
component = computer.GetComponent(&entity.PhysicsComponent{})
computerPhysics := component.(*entity.PhysicsComponent)

if (ballPhysics.Vel.Y > 0 && computerPhysics.Vel.Y < 0) ||
Expand Down
10 changes: 5 additions & 5 deletions psystem/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

// ChangeSystem is the struct responsible to add the observer and handler to the collision event
type ChangeSystem struct {
EntityManager *entity.Manager
Name string
EntityManager *entity.Manager
Name string
CollisionSystem *system.CollisionSystem
}

Expand All @@ -31,7 +31,7 @@ func increaseVel(event system.Event) {
if collision.Ent.GetID() != 2 {
return
}
component, _ := collision.Ent.GetComponent("physics")
component := collision.Ent.GetComponent(&entity.PhysicsComponent{})
physics := component.(*entity.PhysicsComponent)

log.Printf("%v", physics.Vel)
Expand All @@ -51,10 +51,10 @@ func invertAxis(event system.Event) {
if border.Ent.GetID() == 0 {
return
}
component, _ := border.Ent.GetComponent("position")
component := border.Ent.GetComponent(&entity.PositionComponent{})
position := component.(*entity.PositionComponent)

component, _ = border.Ent.GetComponent("physics")
component = border.Ent.GetComponent(&entity.PhysicsComponent{})
physics := component.(*entity.PhysicsComponent)

switch border.Side {
Expand Down
6 changes: 3 additions & 3 deletions psystem/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ func (p *PlayerSystem) Update() {
if button := p.InputManager.Button(); button != empty {
if button.Keysym.Sym == sdl.K_UP {
e := p.EntityManager.Get(0)
p, _ := e.GetComponent("position")
p := e.GetComponent(&entity.PositionComponent{})
pos := p.(*entity.PositionComponent)
if pos.Pos.Y > 0 {
pos.Pos.Y -= 10
}
}
if button.Keysym.Sym == sdl.K_DOWN {
e := p.EntityManager.Get(0)
p, _ := e.GetComponent("position")
p := e.GetComponent(&entity.PositionComponent{})
pos := p.(*entity.PositionComponent)
if pos.Pos.Y < 520 {
pos.Pos.Y += 10
}
}
if button.Keysym.Sym == sdl.K_a && button.State == 0 {
e := p.EntityManager.Get(2)
p, _ := e.GetComponent("physics")
p := e.GetComponent(&entity.PhysicsComponent{})
phs := p.(*entity.PhysicsComponent)
phs.Vel.X *= -1
phs.Vel.Y *= -1
Expand Down
19 changes: 8 additions & 11 deletions psystem/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

// ScoreSystem is the struct responsible to keep the score of the game
type ScoreSystem struct {
Entities []entity.Entitier
EntityManager *entity.Manager
Name string
Entities []entity.Entitier
EntityManager *entity.Manager
Name string
CollisionSystem *system.CollisionSystem
}

Expand All @@ -29,19 +29,15 @@ func (s *ScoreSystem) Init() {
func (s *ScoreSystem) checkScore(event system.Event) {
border := event.(*system.BorderEvent)
obj := border.Ent
var ok bool
var component entity.Component

components := obj.GetComponents()
_, ok = components["position"]
if !ok {
return
}
if border.Side == "right" {
log.Printf("entity: %d", obj.GetID())
log.Println("you scored")

myScore := s.EntityManager.Get(3)
f := myScore.GetComponents()["font"].(*entity.FontComponent)
component = myScore.GetComponent(&entity.FontComponent{})
f := component.(*entity.FontComponent)
score, _ := strconv.Atoi(f.Text)
f.Text = fmt.Sprintf("%d", score+1)
f.Modified = true
Expand All @@ -51,7 +47,8 @@ func (s *ScoreSystem) checkScore(event system.Event) {
log.Println("he scored")

hisScore := s.EntityManager.Get(4)
f := hisScore.GetComponents()["font"].(*entity.FontComponent)
component = hisScore.GetComponent(&entity.FontComponent{})
f := component.(*entity.FontComponent)
score, _ := strconv.Atoi(f.Text)
f.Text = fmt.Sprintf("%d", score+1)
f.Modified = true
Expand Down

0 comments on commit b5c32a1

Please sign in to comment.