Beispiel #1
0
    protected override void OnPositionChanged(Quat rotation, float angle,
                                              Vector3 membraneCoords)
    {
        organelle.OrganelleGraphics.Transform = new Transform(rotation, membraneCoords);

        Vector3 middle = Hex.AxialToCartesian(new Hex(0, 0));
        Vector3 membranePointDirection = (membraneCoords - middle).Normalized();

        membraneCoords += membranePointDirection * Constants.DEFAULT_HEX_SIZE * 2;

        if (organelle.ParentMicrobe.Species.IsBacteria)
        {
            membraneCoords *= 0.5f;
        }

        var physicsRotation = MathUtils.CreateRotationForPhysicsOrganelle(angle);
        var parentMicrobe   = currentShapesParent;

        if (parentMicrobe.Colony != null && !NeedsUpdateAnyway())
        {
            // Get the real position of the pilus while in the colony
            membraneCoords  = organelle.RotatedPositionInsideColony(membraneCoords);
            membraneCoords += parentMicrobe.GetOffsetRelativeToMaster();
        }

        var transform = new Transform(physicsRotation, membraneCoords);

        if (NeedsUpdateAnyway())
        {
            CreateShape(parentMicrobe);
        }

        currentShapesParent.ShapeOwnerSetTransform(addedChildShapes[0], transform);
    }
Beispiel #2
0
    /// <summary>
    ///   Called by a microbe when this organelle has been added to it
    /// </summary>
    public void OnAddedToMicrobe(Microbe microbe)
    {
        if (Definition == null)
        {
            throw new Exception("PlacedOrganelle has no definition set");
        }

        if (ParentMicrobe != null)
        {
            throw new InvalidOperationException("PlacedOrganelle is already in a microbe");
        }

        // Store parameters
        ParentMicrobe = microbe;

        // Grab the species colour for us
        Colour = microbe.Species.Colour;

        ParentMicrobe.OrganelleParent.AddChild(this);

        // Graphical display
        if (Definition.LoadedScene != null)
        {
            SetupOrganelleGraphics();
        }

        float hexSize = Constants.DEFAULT_HEX_SIZE;

        // Scale the physics hex size down for bacteria
        if (microbe.Species.IsBacteria)
        {
            hexSize *= 0.5f;
        }

        // Physics
        ParentMicrobe.Mass += Definition.Mass;

        // Add hex collision shapes
        foreach (Hex hex in Definition.GetRotatedHexes(Orientation))
        {
            var shape = new SphereShape();
            shape.Radius = hexSize * 2.0f;

            var ownerId = ParentMicrobe.CreateShapeOwner(shape);

            // This is needed to actually add the shape
            ParentMicrobe.ShapeOwnerAddShape(ownerId, shape);

            // The shape is in our parent so the final position is our
            // offset plus the hex offset
            Vector3 shapePosition = Hex.AxialToCartesian(hex) + Translation;

            // Scale for bacteria physics.
            if (microbe.Species.IsBacteria)
            {
                shapePosition *= 0.5f;
            }

            var transform = new Transform(Quat.Identity, shapePosition);
            ParentMicrobe.ShapeOwnerSetTransform(ownerId, transform);

            shapes.Add(ownerId);
        }

        // Components
        Components = new List <IOrganelleComponent>();

        foreach (var factory in Definition.ComponentFactories)
        {
            var component = factory.Create();

            if (component == null)
            {
                throw new Exception("PlacedOrganelle component factory returned null");
            }

            component.OnAttachToCell(this);

            Components.Add(component);
        }

        ResetGrowth();
    }
Beispiel #3
0
    /// <summary>
    ///   Called by a microbe when this organelle has been added to it
    /// </summary>
    public void OnAddedToMicrobe(Microbe microbe)
    {
        if (Definition == null)
        {
            throw new Exception("PlacedOrganelle has no definition set");
        }

        if (ParentMicrobe != null)
        {
            throw new InvalidOperationException("PlacedOrganelle is already in a microbe");
        }

        // Store parameters
        ParentMicrobe = microbe;

        // Grab the species colour for us
        Colour = microbe.Species.Colour;

        ParentMicrobe.OrganelleParent.AddChild(this);

        // Graphical display
        if (Definition.LoadedScene != null)
        {
            // There is an intermediate node so that the organelle scene root rotation and scale work
            OrganelleGraphics = new Spatial();
            var organelleSceneInstance = (Spatial)Definition.LoadedScene.Instance();

            AddChild(OrganelleGraphics);

            OrganelleGraphics.Scale = new Vector3(Constants.DEFAULT_HEX_SIZE, Constants.DEFAULT_HEX_SIZE,
                                                  Constants.DEFAULT_HEX_SIZE);

            var transform = new Transform(MathUtils.CreateRotationForOrganelle(Orientation),
                                          Definition.CalculateModelOffset());
            OrganelleGraphics.Transform = transform;

            // Store the material of the organelle to be updated
            GeometryInstance geometry;

            // Fetch the actual model from the scene to get at the material we set the tint on
            if (string.IsNullOrEmpty(Definition.DisplaySceneModelPath))
            {
                geometry = (GeometryInstance)organelleSceneInstance;
            }
            else
            {
                geometry = organelleSceneInstance.GetNode <GeometryInstance>(Definition.DisplaySceneModelPath);
            }

            // Store animation player for later use
            if (!string.IsNullOrEmpty(Definition.DisplaySceneAnimation))
            {
                OrganelleAnimation = organelleSceneInstance.GetNode <AnimationPlayer>(Definition.DisplaySceneAnimation);
            }

            organelleMaterial = (ShaderMaterial)geometry.MaterialOverride;

            OrganelleGraphics.AddChild(organelleSceneInstance);
        }

        // Position relative to origin of cell
        RotateY(Orientation * 60);
        Translation = Hex.AxialToCartesian(Position);

        float hexSize = Constants.DEFAULT_HEX_SIZE;

        // Scale the physics hex size down for bacteria
        if (microbe.Species.IsBacteria)
        {
            hexSize *= 0.5f;
        }

        // Physics
        ParentMicrobe.Mass += Definition.Mass;

        // Add hex collision shapes
        foreach (Hex hex in Definition.GetRotatedHexes(Orientation))
        {
            var shape = new SphereShape();
            shape.Radius = hexSize * 2.0f;

            var ownerId = ParentMicrobe.CreateShapeOwner(shape);

            // This is needed to actually add the shape
            ParentMicrobe.ShapeOwnerAddShape(ownerId, shape);

            // The shape is in our parent so the final position is our
            // offset plus the hex offset
            Vector3 shapePosition = Hex.AxialToCartesian(hex) + Translation;

            // Scale for bacteria physics.
            if (microbe.Species.IsBacteria)
            {
                shapePosition *= 0.5f;
            }

            var transform = new Transform(Quat.Identity, shapePosition);
            ParentMicrobe.ShapeOwnerSetTransform(ownerId, transform);

            shapes.Add(ownerId);
        }

        // Components
        Components = new List <IOrganelleComponent>();

        foreach (var factory in Definition.ComponentFactories)
        {
            var component = factory.Create();

            if (component == null)
            {
                throw new Exception("PlacedOrganelle component factory returned null");
            }

            component.OnAttachToCell(this);

            Components.Add(component);
        }

        ResetGrowth();
    }