Trying to convert Rainy Day Tutorial to TGB.
by Glenn Thomas · in Torque Game Builder · 01/02/2008 (12:59 am) · 6 replies
I'm pretty sure I screwed this all up. I'm not really a programmer but I'm trying to learn. That's enough background info for now, so here's the point. I've been trying to learn behaviors and torque script. So I wanted to convert the the Rainy Day tutorial for Torque X into something TGB users could utilize. I first attempted to convert this tutorial into to behaviors to get an effect similar to the takes damage and deals damage behavior relationship that Garage Games so kindly provided to learn from, but my feeble mind got lost and confused. Since Torque X is basically raw C# and TGB is torque script I got confused. The plan was to utilize the code from the tutorial and separate it. One behavior would handle the flower animation and growth specs while the other behavior would handle the water colliding and tell the flowers when to grow and die.
...I got confused on how to break it up and after numerous attempts of no effect I run to the community for help.
Here's my behavior code, ignore the variable descriptions.
continued below...
...I got confused on how to break it up and after numerous attempts of no effect I run to the community for help.
Here's my behavior code, ignore the variable descriptions.
if (!isObject(PlantGrowthBehavior))
{
%template = new BehaviorTemplate(PlantGrowthBehavior);
%template.friendlyName = "Plant Growth";
%template.behaviorType = "Game";
%template.description = "This is a behavior to make the flowers grow in the Rainy Day tutorial";
%template.addBehaviorField(moisture, "The amount of damage the object deals", float, 10);
%template.addBehaviorField(maxMoisture, "Delete the object when it collides", float, 10);
%template.addBehaviorField(minMoisture, "The particle effect to play on collision", float, 0);
%template.addBehaviorField(lowMoistureThreshold, "The amount of damage the object deals", float, 5);
%template.addBehaviorField(dryRate, "Delete the object when it collides", float, 0.05);
%template.addBehaviorField(wateringRate, "The amount of damage the object deals", float, 0.05);
%template.addBehaviorField(watering, "Delete the object when it collides", bool, false);
%template.addBehaviorField(growAnim, "The particle effect to play on collision", object, "", t2dAnimatedSprite);
%template.addBehaviorField(dieAnim, "The particle effect to play on collision", object, "", t2dAnimatedSprite);
}
function PlantGrowthBehavior::onLevelLoaded(%this, %scenegraph)
{
%this.dieAnim = AnimatedSprite.AnimationData;
%this.growAnim = AnimatedSprite.AnimationData.Clone() as T2DAnimationData;
List<string> %frames = new List<string>(%this.growAnim.AnimationFrames.Split(' '));
%frames.Reverse();
%this.growAnim.AnimationFrames = string.Join(" ", %frames.ToArray());
}
function PlantGrowthBehavior::RunAnimation(%this, %direction)
{
if (%direction < 0.0f)
{
if (AnimatedSprite.AnimationData != %this.growAnim)
{
%frame = AnimatedSprite.FinalFrame - AnimatedSprite.CurrentFrame;
AnimatedSprite.PlayAnimation(%this.growAnim);
AnimatedSprite.SetAnimationFrame((uint)%frame);
}
}
else
{
if (AnimatedSprite.AnimationData != %this.dieAnim)
{
%frame = AnimatedSprite.FinalFrame - AnimatedSprite.CurrentFrame;
AnimatedSprite.PlayAnimation(%this.dieAnim);
AnimatedSprite.SetAnimationFrame((uint)%frame);
}
}
}continued below...
#2
01/02/2008 (5:09 pm)
I actually am going through the process of converting some of the Torque X tutorials for TGB and Rainy Day is one of them. All I have left to do is to change out the Torque X screen shots with the TGB ones but if you like I can either post my code up here or email it to you. Keep in mind I am not a developer so the code won't be perfect, this is just something that I am doing in my spare work hours to better understand coding, development practices and our engines.
#3
01/02/2008 (7:23 pm)
Cool I'd love to see the code! It'll help me greatly and I could possibly tackle the other tutorials a little easier. It would be best to post the code here so the community could benefit.
#4
01/03/2008 (10:23 am)
Here is the code I used for creating the new animation datablock from the original die animation://-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
if (!isObject(PlantGrowthBehavior))
{
%template = new BehaviorTemplate(PlantGrowthBehavior);
%template.friendlyName = "Plant Growth Behavior";
%template.behaviorType = "Management";
%template.description = "Controls the growth of the plants";
%template.addBehaviorField(Moisture, "", float, 10.0);
%template.addBehaviorField(minMoisture, "The lowest amount of moisture for the plant", float, 0.0);
%template.addBehaviorField(maxMoisture, "The largest amount of moisture for the plant", float, 10.0);
%template.addBehaviorField(LowMoistureThreshold, "The threshold that causes it to start growing or dieing", float, 5.0);
%template.addBehaviorField(DryRate, "How fast it loses moisture", float, 0.05);
%template.addBehaviorField(WateringRate, "How fast it gains moisture", float, 0.05);
}
function PlantGrowthBehavior::onAddtoScene(%this)
{
%this.dieAnim = %this.owner.getAnimation();
%j = 0;
for (%i = getWordCount(%this.dieAnim.animationFrames) - 1; %i >= 0; %i--)
{
%growAnim = setWord(%growAnim, %j, getWord(%this.dieAnim.animationFrames, %i));
%j++;
}
%this.growAnim = new t2dAnimationDatablock()
{
animationCycle = %this.dieAnim.animationCycle;
animationFrames = %growAnim;
animationTime = %this.dieAnim.animationTime;
imageMap = %this.dieAnim.imageMap;
randomStart = %this.dieAnim.randomStart;
startFrame = %this.dieAnim.startFrame;
};
%this.owner.enableUpdateCallback();
}
function PlantGrowthBehavior::runAnimation(%this, %direction)
{
if (%direction < 0.0)
{
if (%this.owner.getAnimation() != %this.growAnim)
{
%frame = (getWordCount(%this.growAnim.AnimationFrames) - 1) - %this.owner.getAnimationFrame();
%this.owner.PlayAnimation(%this.growAnim);
%this.owner.setAnimationFrame(%frame);
}
} else
{
if (%this.owner.getAnimation() != %this.dieAnim)
{
%frame = (getWordCount(%this.dieAnim.AnimationFrames) - 1) - %this.owner.getAnimationFrame();
%this.owner.PlayAnimation(%this.dieAnim);
%this.owner.setAnimationFrame(%frame);
}
}
}
function PlantGrowthBehavior::onUpdate(%this)
{
if (%this.moisture < %this.lowMoistureThreshold)
{
// dying -- run animation forwards.
%this.RunAnimation(1.0);
} else
{
// growing -- run animation backwards.
%this.RunAnimation(-1.0);
}
// modify amount of moisture based on whether we're being watered or not
if (%this.watering)
%this.moisture += %this.wateringRate;
else
%this.moisture -= %this.dryRate;
// clamp moisture amounts.
if (%this.moisture < %this.minMoisture)
%this.moisture = %this.minMoisture;
if (%this.moisture > %this.maxMoisture)
%this.moisture = %this.maxMoisture;
}
function PlantGrowthBehavior::startWatering(%this)
{
%this.watering = true;
}
function PlantGrowthBehavior::endWatering(%this)
{
%this.watering = false;
}
#5
Hope that helps! If anyone has any questions or knows of a cleaner way in which I could have done this in TGB I would love to hear it as I am just a beginner and want to hear of better ways to do things.
01/03/2008 (10:26 am)
I also created a seperate behavior that I attached to the Trigger Object to call startWatering and endWatering at the appropriate times.//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
if (!isObject(WateringBehavior))
{
%template = new BehaviorTemplate(WateringBehavior);
%template.friendlyName = "Watering Behavior";
%template.behaviorType = "Trigger";
%template.description = "Defines when to begin watering";
}
function WateringBehavior::onEnter(%this, %object)
{
%object.startWatering();
}
function WateringBehavior::onLeave(%this, %object)
{
%object.endWatering();
}Hope that helps! If anyone has any questions or knows of a cleaner way in which I could have done this in TGB I would love to hear it as I am just a beginner and want to hear of better ways to do things.
#6
01/03/2008 (11:30 am)
Thanks alot! I was on some other planet with this one but you helped me learn quite a bit!
Torque Owner Glenn Thomas
Function PlantGrowthBehavior::CompareMoisture(%this) { if (%this.moisture < %this.lowMoistureThreshold) { // dying -- run animation forwards. RunAnimation(1.0f); } else { // growing -- run animation backwards. RunAnimation(-1.0f); } // modify amount of moisture based on whether we're being watered or not. if (%this.watering) %this.moisture += %this.wateringRate; else %this.moisture -= %this.dryRate; // clamp moisture amounts. if (%this.moisture < %this.minMoisture) %this.moisture = %this.minMoisture; if (%this.moisture > %this.maxMoisture) %this.moisture = %this.maxMoisture; } function PlantGrowthBehavior::Grow(%this, %ourObject, %theirObject) { %plant = theirObject.getBehavoir("PlantGrowthBehavior"); if (plant != null) plant.watering = true; } function PlantGrowthBehavior::Die(%this, %ourObject, %theirObject) { %plant = theirObject.getBehavoir("PlantGrowthBehavior"); if (plant == null) plant.watering = false; } function PlantGrowthBehavior::BeginWatering(%this) { get { return Grow; } } function PlantGrowthBehavior::EndWatering(%this) { get { return Die; } }I know I probably should of made another flower with a growing animation to make this easier but I wanted to know if the code for reversing the animation and flip flopping mid frame actually worked in TGB as well.