TGB/Tutorials/Rainy Day
From TDN
[edit] Rainy Day TutorialWritten for TGB Version: 1.6 |
|
[edit] Getting Started
[edit] Background Information
|
![]() |
|
|

|
[edit] Flower Power
|
![]() |
|
|
|
[edit] Into every life a little rain must fall
|
![]() |
|
|

|
if (!isObject(MovementBehavior))
{
%template = new BehaviorTemplate(MovementBehavior);
%template.friendlyName = "Movement Behavior";
%template.behaviorType = "Movement Styles";
%template.description = "Shooter style movement control";
%template.addBehaviorField(upKey, "Key to bind to upward movement", keybind, "keyboard up");
%template.addBehaviorField(downKey, "Key to bind to downward movement", keybind, "keyboard down");
%template.addBehaviorField(leftKey, "Key to bind to left movement", keybind, "keyboard left");
%template.addBehaviorField(rightKey, "Key to bind to right movement", keybind, "keyboard right");
%template.addBehaviorField(verticalSpeed, "Speed when moving vertically", float, 20.0);
%template.addBehaviorField(horizontalSpeed, "Speed when moving horizontally", float, 20.0);
}
function MovementBehavior::onBehaviorAdd(%this)
{
if (!isObject(moveMap))
return;
// bind our keys to the keyboard
moveMap.bindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), "moveUp", %this);
moveMap.bindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), "moveDown", %this);
moveMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), "moveLeft", %this);
moveMap.bindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), "moveRight", %this);
// set the default values to 0
%this.up = 0;
%this.down = 0;
%this.left = 0;
%this.right = 0;
}
function MovementBehavior::onBehaviorRemove(%this)
{
if (!isObject(moveMap))
return;
%this.owner.disableUpdateCallback();
// remove the keybinds
moveMap.unbindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), %this);
moveMap.unbindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), %this);
moveMap.unbindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), %this);
moveMap.unbindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), %this);
%this.up = 0;
%this.down = 0;
%this.left = 0;
%this.right = 0;
}
function MovementBehavior::updateMovement(%this)
{
// make the player move
%this.owner.setLinearVelocityX((%this.right - %this.left) * %this.horizontalSpeed);
%this.owner.setLinearVelocityY((%this.down - %this.up) * %this.verticalSpeed);
}
function MovementBehavior::moveUp(%this, %val)
{
%this.up = %val;
%this.updateMovement();
}
function MovementBehavior::moveDown(%this, %val)
{
%this.down = %val;
%this.updateMovement();
}
function MovementBehavior::moveLeft(%this, %val)
{
%this.left = %val;
%this.updateMovement();
}
function MovementBehavior::moveRight(%this, %val)
{
%this.right = %val;
%this.updateMovement();
}
[edit] This plant is operating within acceptable parameters
|
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 dying", 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);
}
|
[edit] It's a matter of life and death
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;
}
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 you like the tutorial. If you could please fill out this Survey it helps us create better tutorials and documentation for you guys!
Return to Tutorial Hub












