Files
WickedEngine/Tests/test_script.lua
T
2018-10-01 18:30:14 +01:00

76 lines
2.0 KiB
Lua

-- Wicked Engine Test Framework lua script
debugout("Begin script: test_script.lua");
-- Load a model:
LoadModel("../models/teapot.wiscene");
-- Load an image:
local sprite = Sprite("images/HelloWorld.png");
sprite.SetEffects(ImageEffects(100,100,100,50));
-- Set this image as renderable to the active component:
local component = main.GetActiveComponent();
component.AddSprite(sprite);
-- Start a background task to rotate the model and update the sprite:
runProcess(function()
local velocity = Vector((math.random() * 2 - 1) * 4, (math.random() * 2 - 1) * 4); -- starting velocity for our sprite
local screenW = GetScreenWidth();
local screenH = GetScreenHeight();
local scene = GetScene();
-- This shows how to handle attachments:
-- Create parent transform, this will be rotated:
local parent = CreateEntity();
scene.Component_CreateTransform(parent);
-- Retrieve teapot base and lid entity IDs:
local teapot_base = scene.Entity_FindByName("Base");
local teapot_top = scene.Entity_FindByName("Top");
-- Attach base to parent, lid to base:
scene.Component_Attach(teapot_base, parent);
scene.Component_Attach(teapot_top, teapot_base);
while(true) do
-- Bounce our sprite across the screen:
local fx = sprite.GetEffects();
local pos = fx.GetPos();
local size = fx.GetSize();
-- if it hits a wall, reverse velocity:
if(pos.GetX()+size.GetX() >= screenW) then
velocity.SetX(velocity.GetX() * -1);
end
if(pos.GetY()+size.GetY() >= screenH) then
velocity.SetY(velocity.GetY() * -1);
end
if(pos.GetX() <= 0) then
velocity.SetX(velocity.GetX() * -1);
end
if(pos.GetY() < 0) then
velocity.SetY(velocity.GetY() * -1);
end
pos = vector.Add(pos, velocity);
fx.SetPos(pos);
sprite.SetEffects(fx);
-- Rotate teapot by parent transform:
local transform = scene.Component_GetTransform(parent);
transform.Rotate(Vector(0, 0.01, 0));
fixedupdate(); -- wait for new frame
end
end);
debugout("Script complete.");