void Start() { rb = GetComponent<Rigidbody2D>(); contactDetector = transform.GetComponentInChildren<ContactDetector>(); player = ReInput.players.GetPlayer(playerId); playerSkin.sprite = playerSkins[playerId]; lives = 3; }
private void Awake() { //rb = GetComponent<Rigidbody>(); cd = GetComponent <ContactDetector>(); moveable = GetComponent <Moveable>(); sound = GetComponent <AudioSource>(); boxCollider = GetComponent <BoxCollider>(); if (boxCollider != null && transform.name != "cauldron_LID") { boxCollider.size = big ? new Vector3(0.97f, 0.97f, 1f) : new Vector3(0.9f, 0.9f, 1f); } }
protected BipedalWalkerEnv(bool hardcore) : base(new Box(new[] { -1.0f, -1.0f, -1.0f, -1.0f }, new[] { 1.0f, 1.0f, 1.0f, 1.0f }, new Shape(4)), new Box(float.NegativeInfinity, float.PositiveInfinity, new Shape(24))) { this.hardcore = hardcore; contactDetector = new ContactDetector(this); var worldAabb = new b2AABB() { lowerBound = new b2Vec2(-100, -100), upperBound = new b2Vec2(1000, 1000) }; World = new b2World(new b2Vec2(0, -9.807f)); HULL_FD.shape = new b2PolygonShape(); (HULL_FD.shape as b2PolygonShape).Set(HULL_POLY.Select(v => new b2Vec2(v[0] / SCALE, v[1] / SCALE)).ToArray()); HULL_FD.density = 5.0f; HULL_FD.friction = 0.1f; HULL_FD.filter.categoryBits = 0x0020; HULL_FD.filter.maskBits = 0x0001; // collide only with ground HULL_FD.restitution = 0; // 0.99 bouncy LEG_FD.shape = new b2PolygonShape(); (LEG_FD.shape as b2PolygonShape).SetAsBox(LEG_W / 2, LEG_H / 2); LEG_FD.density = 1.0f; LEG_FD.filter.categoryBits = 0x0020; LEG_FD.filter.maskBits = 0x0001; LEG_FD.restitution = 0; LOWER_FD.shape = new b2PolygonShape(); (LOWER_FD.shape as b2PolygonShape).SetAsBox(0.8f * LEG_W / 2, LEG_H / 2); LOWER_FD.density = 1.0f; LOWER_FD.filter.categoryBits = 0x0020; LOWER_FD.filter.maskBits = 0x0001; LOWER_FD.restitution = 0; fd_polygon.shape = new b2PolygonShape(); (fd_polygon.shape as b2PolygonShape).Set(new[] { new b2Vec2(0, 0), new b2Vec2(1, 0), new b2Vec2(1, -1), new b2Vec2(0, -1) }); fd_polygon.friction = FRICTION; fd_edge.shape = new b2EdgeShape(); (fd_edge.shape as b2EdgeShape).Set(new b2Vec2(0, 0), new b2Vec2(1, 1)); fd_edge.friction = FRICTION; fd_edge.filter.categoryBits = 0x0001; Reset(); }
private void Awake() { changeable = GetComponent <Changeable>(); rb = GetComponent <Rigidbody>(); cd = GetComponent <ContactDetector>(); }
public void Change(EvalContext context, ChangeType type) { Entity target = context.Target; Changeable changeable = target.GetComponent <Changeable>(); BoxCollider collider = target.GetComponent <BoxCollider>(); if (changeable == null) { //Debug.LogError($"Cannot change entity {target} to ChangeType {type}"); return; } if (!changeable.Resizable) { Debug.LogError("Can't change its size"); return; } //Rigidbody rb = target.GetComponent<Rigidbody>(); Moveable moveable = target.GetComponent <Moveable>(); ContactDetector cd = target.GetComponent <ContactDetector>(); Vector3 finalSize = Vector3.one; switch (type) { case ChangeType.Big: if (!changeable.big) { cd.CheckSurroundingObstacles(); if (changeable.IsConfined()) { return; } moveable.Ungravitate(); finalSize = target.transform.localScale * 3; changeable.changing = true; changeable.big = true; changeable.SwellSound(); } else { return; } break; case ChangeType.Small: if (changeable.big) { moveable.Ungravitate(); finalSize = target.transform.localScale / 3; changeable.changing = true; changeable.big = false; changeable.ShrinkSound(); } else { return; } break; default: Debug.LogError($"Unsupported ChangeType {type}"); return; } //RuneStock.Inst.DeductRune(new RuneType(type)); Vector3 originalPosition = target.transform.position; Vector3 finalPosition = (type == ChangeType.Big) ? changeable.BePushed(originalPosition) : originalPosition; StartCoroutine( StartGradualAction(timer => { target.transform.position = Vector3.Lerp( originalPosition, finalPosition, timer ); target.transform.localScale = Vector3.Lerp( target.transform.localScale, finalSize, timer); //Debug.Log($"({Mathf.Round(10 * (-5 - target.transform.position.x)/timer)/10}, { (target.transform.position.y + 2) / timer} kinematic:{target.GetComponent<Rigidbody>().isKinematic} { moveable.YTendency })"); }, () => { target.transform.localScale = finalSize; changeable.changing = false; changeable.AdjustPosition(); collider.size = type == ChangeType.Big ? new Vector3(0.97f, 0.97f, 0.97f) : new Vector3(0.9f, 0.9f, 0.9f); }, 1.0f ) ); }
public override Tensor Reset() { State = new Tensor(ObservationSpace.Shape); Destroy(); contact_detector = new ContactDetector(this); world.SetContactListener(contact_detector); game_over = false; prev_shaping = float.NaN; var W = VIEWPORT_W / SCALE; var H = VIEWPORT_H / SCALE; // terrain var CHUNKS = 11; var height = Rng.NextFloat(0.0f, H / 2.0f, CHUNKS + 1); var chunk_x = Enumerable.Range(0, CHUNKS).Select(i => W / (CHUNKS - 1) * i).ToArray(); helipad_x1 = chunk_x[(int)Math.Floor(CHUNKS / 2.0) - 1]; helipad_x2 = chunk_x[(int)Math.Floor(CHUNKS / 2.0) + 1]; helipad_y = H / 4; height[(int)Math.Floor(CHUNKS / 2.0) - 2] = helipad_y; height[(int)Math.Floor(CHUNKS / 2.0) - 1] = helipad_y; height[(int)Math.Floor(CHUNKS / 2.0) + 0] = helipad_y; height[(int)Math.Floor(CHUNKS / 2.0) + 1] = helipad_y; height[(int)Math.Floor(CHUNKS / 2.0) + 2] = helipad_y; var smooth_y = Enumerable.Range(0, CHUNKS).Select(i => 0.33f * (height[(CHUNKS + i - 1) % CHUNKS] + height[i + 0] + height[i + 1])).ToArray(); moon = world.CreateStaticBody(new b2FixtureDef() { shape = new b2EdgeShape().Set(new b2Vec2(0, 0), new b2Vec2(W, 0)) }); sky_polys.Clear(); for (int i = 0; i < CHUNKS - 1; ++i) { var p1 = new float[] { chunk_x[i], smooth_y[i] }; var p2 = new float[] { chunk_x[i + 1], smooth_y[i + 1] }; moon.CreateFixture(new b2FixtureDef() { shape = new b2EdgeShape().Set(new b2Vec2((float)p1[0], (float)p1[1]), new b2Vec2((float)p2[0], (float)p2[1])), density = 0, friction = 0.1f }); sky_polys.Add(new List <float[]> { p1, p2, new[] { p2[0], H }, new[] { p1[0], H } }); } moon.SetUserData(new CustomBodyData() { Color1 = new b2Vec3(0, 0, 0), Color2 = new b2Vec3(0, 0, 0) }); var initial_y = VIEWPORT_H / SCALE; lander = world.CreateDynamicBody(new b2Vec2(VIEWPORT_W / SCALE / 2, initial_y), 0.0f, new b2FixtureDef() { shape = new b2PolygonShape().Set(LANDER_POLY.Select(p => new b2Vec2(p[0] / SCALE, p[1] / SCALE)).ToArray()), density = 5.0f, friction = 0.1f, filter = new b2Filter() { categoryBits = 0x0010, maskBits = 0x001 }, // collide only with ground restitution = 0.0f } // 0.99 bouncy ); lander.SetUserData(new CustomBodyData() { Color1 = new b2Vec3(0.5f, 0.4f, 0.9f), Color2 = new b2Vec3(0.3f, 0.3f, 0.5f) }); lander.ApplyForceToCenter(new b2Vec2((float)Rng.NextFloat(-INITIAL_RANDOM, INITIAL_RANDOM), (float)Rng.NextFloat(-INITIAL_RANDOM, INITIAL_RANDOM)), true); legs.Clear(); foreach (int i in new[] { -1, +1 }) { var leg = world.CreateDynamicBody( new b2Vec2(VIEWPORT_W / SCALE / 2 - i * LEG_AWAY / SCALE, initial_y), i * 0.05f, new b2FixtureDef() { shape = new b2PolygonShape().SetAsBox(LEG_W / SCALE, LEG_H / SCALE), density = 1.0f, restitution = 0.0f, filter = new b2Filter() { categoryBits = 0x0020, maskBits = 0x001 } } ); leg.SetUserData(new CustomBodyData() { Color1 = new b2Vec3(0.5f, 0.4f, 0.9f), Color2 = new b2Vec3(0.3f, 0.3f, 0.5f) }); var rjd = new b2RevoluteJointDef() { bodyA = lander, bodyB = leg, localAnchorA = new b2Vec2(0, 0), localAnchorB = new b2Vec2(i * LEG_AWAY / SCALE, LEG_DOWN / SCALE), enableMotor = true, enableLimit = true, maxMotorTorque = LEG_SPRING_TORQUE, motorSpeed = +0.3f * i // low enough not to jump back into the sky }; if (i == -1) { rjd.lowerAngle = +0.9f - 0.5f; // Yes, the most esoteric numbers here, angles legs have freedom to travel within rjd.upperAngle = +0.9f; } else { rjd.lowerAngle = -0.9f; rjd.upperAngle = -0.9f + 0.5f; } world.CreateJoint(rjd); legs.Add(leg); } drawlist = new List <b2Body> { lander }; drawlist = drawlist.Concat(legs).ToList(); Step(continuous ? new Tensor(new[] { 0.0f, 0.0f }, new Shape(2)) : new Tensor(new[] { 0.0f }, new Shape(1)), out var observation, out var reward); return(observation); }