public override void OnBlockObjectCreated(BlockInstance instance, GameObject obj, BlockData block) { var explosion = obj.AddComponent <ExplosionBlockInstance>(); explosion.BlockInstance = instance; explosion.BlockData = block; instance.GetData <Data>().Blocks.Set(block.Position, explosion); }
public override void UpdateInstance(BlockInstance instance) { var data = instance.GetData <MotionData>(); if (HitForward(instance)) { if (HitBackward(instance)) { data.velocity = Vector2.zero; } else if (Mathf.Approximately(data.velocity.magnitude, 0)) { data.velocity = (Direction == MoveDirection.Horizontal ? Vector2.left : Vector2.down) * Speed; } else { data.velocity = -data.velocity; } } else if (Mathf.Approximately(data.velocity.magnitude, 0)) { data.velocity = (Direction == MoveDirection.Horizontal ? Vector2.left : Vector2.down) * Speed; } instance.GetComponent <Rigidbody2D>().velocity = data.velocity; /* * if (MergeMode == BlockMergeMode.Both) * { * * } * else * { * var pos = instance.transform.position.ToVector2() + instance.BoxCollider.offset; * var dir = instance.BoxCollider.size / 2 * data.velocity.normalized; * var count = Physics2D.RaycastNonAlloc(pos, dir.normalized, hits, dir.magnitude, 1 << 11); * Debug.DrawLine(pos, pos + dir, Color.red); * for (int i = 0; i < count; i++) * { * if (hits[i].collider.isTrigger) * continue; * if (hits[i].rigidbody == instance.GetComponent<Rigidbody2D>()) * continue; * var block = hits[i].rigidbody?.GetComponent<IBlockInstance>()?.GetContactedBlock(hits[i].point, hits[i].normal); * if (block) * { * data.velocity = -data.velocity; * break; * } * } * * }*/ }
bool HitForward(BlockInstance instance) { var data = instance.GetData <MotionData>(); var dir = data.velocity.normalized * .5f; if (Mathf.Approximately(data.velocity.magnitude, 0)) { dir = this.Direction == MoveDirection.Horizontal ? Vector2.right : Vector2.up; } dir += dir.normalized * 0.01f; for (var blockIdx = 0; blockIdx < instance.Blocks.BlocksList.Count; blockIdx++) { var block = instance.Blocks.BlocksList[blockIdx]; var pos = block.Position.ToVector3() - instance.Blocks.Bound.center + instance.transform.position + new Vector3(.5f, .5f, 0); var count = Physics2D.RaycastNonAlloc(pos, dir.normalized, hits, dir.magnitude, 1 << 11); Debug.DrawLine(pos, pos + dir.ToVector3(), Color.red); for (int i = 0; i < count; i++) { if (hits[i].collider.isTrigger) { continue; } if (hits[i].rigidbody == instance.GetComponent <Rigidbody2D>()) { continue; } var hitBlock = hits[i].rigidbody?.GetComponent <IBlockInstance>()?.GetContactedBlock(hits[i].point, hits[i].normal); if (hitBlock) { return(true); } } } return(false); }