void CreateDecor(Vector3 position, Vector3 leftVector) { _discard++; if (_discard % 3 != 0) { return; } double chance = _noise.Evaluate(position.X * 0.2f, 0) + 1; Node n = null; bool isTree = false; if (chance < 1.2f) { //grasses int r = _rng.Next(6); if (r < 4) { n = Racer2D.CreateSpriteNode(_decors[r], 3, false); } } else if (chance < 1.3f) { int r = _rng.Next(4) + 4; n = Racer2D.CreateSpriteNode(_decors[r], 4, false); } else if (chance < 1.7f) { if (Math.Abs(leftVector.Y) < 0.1f) //Vector3.Dot(leftVector, Vector3.Left)) { int r = _rng.Next(3) + 8; n = Racer2D.CreateSpriteNode(_decors[r], 4, false); n.Scale2D = n.Scale2D *= 1 + _rng.Next(30) / 100f; isTree = true; } } if (n != null) { n.SetPosition(position + Vector3.Forward * 15); if (!isTree) { Quaternion transformation = Quaternion.FromRotationTo(Vector3.Left, leftVector); //todo use atan2?? n.SetRotation(transformation); } else { //correct offset hack fixme n.Translate(-n.Up * 0.1f); } } }
// Emit particles if close to a surface point and returns true if positive public bool EmitSurfaceParticles() { Vector3 nearestSurfPoint = Racer2D.GetSurfacePointClosestToPoint(_rigidBody.Node); float contactDistance = Vector3.Distance(_rigidBody.Node.Position, nearestSurfPoint); if (contactDistance > _particlesDistance) { _particleEmitter.Effect.StartColor = new Color(0, 0, 0, 0); return(false); } _particleEmitter.Effect.StartColor = Color.White; _particleEmitter.Node.Position = nearestSurfPoint; return(true); }
public Terrain(Scene scene) { _scene = scene; _chunkmat = Cache.Get <Material>("_DBG/Unlit.xml"); _chunkmat.SetTexture(0, Cache.Get <Texture2D>("scenarios/grasslands/ground.png")); _surfMat = Cache.Get <Material>("_DBG/UnlitAlpha.xml"); _surfMat.SetTexture(0, Cache.Get <Texture2D>("scenarios/grasslands/surface.png")); // _decors quick hack _decors.Add(Cache.Get <Sprite2D>("scenarios/grasslands/Object/Bush (1).png")); _decors.Add(Cache.Get <Sprite2D>("scenarios/grasslands/Object/Bush (2).png")); _decors.Add(Cache.Get <Sprite2D>("scenarios/grasslands/Object/Bush (3).png")); _decors.Add(Cache.Get <Sprite2D>("scenarios/grasslands/Object/Bush (4).png")); _decors.Add(Cache.Get <Sprite2D>("scenarios/grasslands/Object/Mushroom_1.png")); _decors.Add(Cache.Get <Sprite2D>("scenarios/grasslands/Object/Mushroom_2.png")); _decors.Add(Cache.Get <Sprite2D>("scenarios/grasslands/Object/Stone.png")); _decors.Add(Cache.Get <Sprite2D>("scenarios/grasslands/Object/Sign_2.png")); _decors.Add(Cache.Get <Sprite2D>("scenarios/grasslands/Object/Tree_1.png")); _decors.Add(Cache.Get <Sprite2D>("scenarios/grasslands/Object/Tree_2.png")); _decors.Add(Cache.Get <Sprite2D>("scenarios/grasslands/Object/Tree_3.png")); foreach (Sprite2D d in _decors) { d.SetHotSpot(new Vector2(0.5f, 0.1f)); } Sprite2D crateSprite = Cache.Get <Sprite2D>("scenarios/grasslands/Object/Crate.png"); Random rng = new Random(); for (int i = 0; i < Chunksize * 400; i += Chunksize) { GenerateChunk(i); //boxes if (rng.Next(1) == -10) { var c = Racer2D.CreateSpriteNode(crateSprite, 2); c.SetPosition(new Vector3(i + rng.Next(8), 20, -5)); var box = c.CreateComponent <CollisionBox2D>(); box.SetSize(.8f, .8f); box.SetDensity(0.1f); box.SetRestitution(0.2f); box.SetFriction(0.05f); } } }
public Clouds(float startX, int minY, int deviation, int amount, int range) { _minY = minY; _deviation = deviation; _range = range; Sprite2D[] cloudSprites = { Cache.Get <Sprite2D>("scenarios/cloud1.png"), Cache.Get <Sprite2D>("scenarios/cloud2.png"), Cache.Get <Sprite2D>("scenarios/cloud3.png") }; // We pre-fill the screen with clouds float cloudSpacing = range * 2 / amount; _clouds = new Node[amount]; for (int i = 0; i < amount; i++) { _clouds[i] = Racer2D.CreateSpriteNode(cloudSprites[rng.Next(cloudSprites.Length)], 4, false); RecycleCloud(startX -= cloudSpacing, _clouds[i]); } }
public Vehicle CreateChassis(Vector2 colliderCenter, float colliderRadius, int massDensity, Vector3 exhaustPosition, ParticleEffect2D exhaustParticles, Sound engineSound, Sound tireSound, Sound[] suspensionSounds, int horsePower, int maxSpeedFwd, int maxSpeedBwd, int rollForce) { // We set out private fields _horsePower = horsePower; _maxSpdFwd = maxSpeedFwd; _maxSpdBwd = maxSpeedBwd; _rollForce = rollForce; _rigidBody = GetComponent <RigidBody2D>(); // We add the collider (circle collider at the moment) var col = Racer2D.AddCollider <CollisionCircle2D>(Node, dens: massDensity, fric: 0); col.SetRadius(colliderRadius); col.SetCenter(colliderCenter); // We create the exhaust particle system var exhaustParticlesNode = Node.CreateChild(); exhaustParticlesNode.SetPosition(exhaustPosition); _exhaustParticles = exhaustParticlesNode.CreateComponent <ParticleEmitter2D>(); _exhaustParticles.SetEffect(exhaustParticles); // We setup the engine sound and other sound effect engineSound.SetLooped(true); _soundSource = Node.CreateComponent <SoundSource3D>(); _soundSource.SetNearDistance(10); _soundSource.SetFarDistance(50); _accelSound = engineSound; _brakeSound = tireSound; _suspensionSoundSource = Node.CreateComponent <SoundSource3D>(); _suspensionSounds = suspensionSounds; // We return the Vehicle for convenience, since this function is intended to be the vehicle's init function return(this); }
public Node CreateWheel(Sprite2D sprite, Vector2 relativePosition, float radius, int suspensionFrequency, float suspensionDamping, ParticleEffect2D particles, float distanceToEmitParticles) { Node wheelNode = Racer2D.CreateSpriteNode(sprite); wheelNode.SetPosition2D(relativePosition); // CreateSpriteNode adds a RigidBody for us, so we get it here RigidBody2D wheelRigidBody = wheelNode.GetComponent <RigidBody2D>(); // We activate CCD wheelRigidBody.SetBullet(true); Racer2D.AddCollider <CollisionCircle2D>(wheelNode).SetRadius(radius); // The Box2D wheel joint provides spring for simulating suspension ConstraintWheel2D wheelJoint = Node.CreateComponent <ConstraintWheel2D>(); wheelJoint.SetOtherBody(wheelRigidBody); wheelJoint.SetAnchor(relativePosition); wheelJoint.SetAxis(Vector2.UnitY); wheelJoint.SetFrequencyHz(suspensionFrequency); wheelJoint.SetDampingRatio(suspensionDamping); // Each wheel has a particle emitter to emit particles when it's in contact with the surface Node particlesNode = Node.Scene.CreateChild(); particlesNode.SetPosition(new Vector3(relativePosition.X, relativePosition.Y, 14)); ParticleEmitter2D particleEmitter = particlesNode.CreateComponent <ParticleEmitter2D>(); particleEmitter.SetEffect(particles); // We create a new Wheel struct and add to the _wheels list _wheels.Add(new Wheel(wheelRigidBody, wheelJoint, particleEmitter, distanceToEmitParticles)); return(wheelNode); }
public Node CreateHead(Sprite2D sprite, Vector3 relativePosition, float colliderRadius, Vector2 neckAnchor) { Node head = Racer2D.CreateSpriteNode(sprite); head.SetPosition(relativePosition); Racer2D.AddCollider <CollisionCircle2D>(head).SetRadius(colliderRadius); // This is the actual neck joint ConstraintRevolute2D joint = head.CreateComponent <ConstraintRevolute2D>(); joint.SetOtherBody(_rigidBody); joint.SetAnchor(neckAnchor); // This is the spring, it's attached to the body with an offset ConstraintDistance2D spring = head.CreateComponent <ConstraintDistance2D>(); spring.SetOtherBody(_rigidBody); spring.SetOwnerBodyAnchor(-Vector2.UnitY * 2); spring.SetOtherBodyAnchor(Node.WorldToLocal2D(head.WorldPosition2D - Vector2.UnitY * 2)); spring.SetFrequencyHz(3); spring.SetDampingRatio(0.4f); return(head); }