func NewPhysicsCircle(static bool) *Physics { var body *chipmunk.Body shape := chipmunk.NewCircle(vect.Vector_Zero, 1) if static { body = chipmunk.NewBodyStatic() } else { body = chipmunk.NewBody(1, shape.ShapeClass.Moment(1)) } p := &Physics{BaseComponent: NewComponent(), Body: body, Box: shape.GetAsBox(), Shape: shape} body.AddShape(shape) return p }
func addBall() { x := rand.Intn(350-115) + 115 ball := chipmunk.NewCircle(vect.Vector_Zero, float32(ballRadius)) ball.SetElasticity(0.95) body := chipmunk.NewBody(vect.Float(ballMass), ball.Moment(float32(ballMass))) body.SetPosition(vect.Vect{vect.Float(x), 600.0}) body.SetAngle(vect.Float(rand.Float32() * 2 * math.Pi)) body.AddShape(ball) space.AddBody(body) balls = append(balls, ball) }
func (cc *CharacterController) Update() { if cc.HitPoints <= 0 { //log.Printf("Character Dead, pid=%d", cc.PlayerID) if !cc.deathState { cc.deathState = true PlayDeath := PacketCharacterState{cc.GameObject().GOID, CS_Death} cc.Game.Broadcast2AllPlayersTCP(PlayDeath) } if cc.reviveTimer < (float32)(ReviveTimer) { cc.reviveTimer += FixedTimeStep return } else { cc.reviveTimer = 0 cc.Revive() } } if cc.Input.Space == 1 && cc.Input.Space != cc.Input.lastSpaceState { //broadcast attack state PlayAttack := PacketCharacterState{cc.GameObject().GOID, CS_Attack} cc.Game.Broadcast2OtherTCP(PlayAttack, cc.PlayerID, false) attack := NewGameObject("attack") physics := attack.AddComponent(NewPhysicsShape(false, cc.Physics.Space, chipmunk.NewCircle(vect.Vector_Zero, float32(0.15)))).(*Physics) physics.Shape.IsSensor = true physics.Body.IgnoreGravity = true var v Vector = cc.Transform().Position() if cc.Transform().Rotation().Y == 0 { // l = 180 r =0 v.X += 0.06 } else { v.X -= 0.06 } attack.Transform().SetPosition(v) attack.AddComponent(NewProjectile(cc, physics, 0.1)) cc.Game.AddGameObject(attack) } cc.Input.lastSpaceState = cc.Input.Space switch cc.Direction.Y { case 1: { //if cc.Ground != nil && cc.Physics.Body.Velocity().Y <= 0 { // cc.Velocity.Y = 5 //} if cc.Ground != nil && cc.Physics.Body.Velocity().Y == 0 { cc.Physics.Body.AddForce(0, 325) } } case -1: { if cc.Ground != nil && cc.Ground.GameObject().Name()[0:3] == "pla" { //log.Printf("s") } } } var maxInertion float32 = MaxInertion // MaxShiftInertion if cc.Input.Shift == 1 { maxInertion = MaxShiftInertion } switch cc.Direction.X { case 1: { if cc.inertion < maxInertion { cc.inertion++ } if cc.Input.Shift == 0 && cc.inertion > maxInertion { cc.inertion-- } } case -1: { if cc.inertion > -maxInertion { cc.inertion-- } if cc.Input.Shift == 0 && cc.inertion < -maxInertion { cc.inertion++ } } default: { if cc.inertion != 0 { if cc.inertion > 0 { cc.inertion-- } else { cc.inertion++ } } } } cc.Velocity.X = MoveSpeed * cc.inertion * FixedTimeStep cc.Physics.Body.SetVelocityX(cc.Velocity.X) //cc.Physics.Body.SetVelocity(cc.Velocity.X, cc.Velocity.Y) s := cc.Transform().Rotation() if cc.Direction.X < 0 && s.Y != 180 { s.Y = 180 } else if cc.Direction.X > 0 && s.Y != 0 { s.Y = 0 } cc.Transform().SetRotation(s) }