2D-Entitysystem

Previous topic - Next topic

Schranz0r

Hi, i start a 2D-Entitysystem with the new "Function in Type"-Methods :)
I think i will invest more time in this, and then i write a little Game hopefully   :-*

Maybe its a good start for others to make a game... ?

Anyway, here my code:

Code (glbasic) Select
// --------------------------------- //
// Project: 2D Entitysystem
// Start: Saturday, October 09, 2010
// IDE Version: 8.125

TYPE TSprite
id%
name$
ENDTYPE

GLOBAL T2DEntityArray[] AS T2DEntity  // if someone need this?
GLOBAL __white__% = RGB(0xff, 0xff, 0xff)

TYPE T2DEntity
spr AS TSprite
parent AS TSprite

pos%[2]
x%;y% // rotation 0 = self , 1 = around the parent
sx%;sy% // size
hx%;hy%  // x-y position of the handle!
phi# = 0
scale# = 1
col% = __white__

FUNCTION Load: name$
self.spr.id = GENSPRITE()
self.spr.name$ = name$

LOADSPRITE self.spr.name$, self.spr.id
GETSPRITESIZE self.spr.id, self.sx, self.sy

IF self.sx > 0 OR self.sy > 0 // check of any problems
RETURN TRUE
ELSE
RETURN FALSE  // all fine!
ENDIF

ENDFUNCTION

FUNCTION SetPosition: x, y
self.x = x
self.y = y
ENDFUNCTION

FUNCTION Draw:
_RotoZoomPoly(self.spr.id,self.spr.id, self.x, self.y, self.phi, self.scale, self.col, self.hx, self.hy, self.sx, self.sy)
ENDFUNCTION

FUNCTION PushBack:
DIMPUSH T2DEntityArray[], self
ENDFUNCTION

FUNCTION CopyToEntity: newcopy AS T2DEntity
newcopy = self
ENDFUNCTION

FUNCTION GetFilename$:
RETURN self.spr.name$
ENDFUNCTION

FUNCTION Get_X:
RETURN self.x
ENDFUNCTION

FUNCTION Get_Y:
RETURN self.y
ENDFUNCTION

FUNCTION Get_Size_X:
RETURN self.sx
ENDFUNCTION

FUNCTION GET_Size_Y:
RETURN self.sy
ENDFUNCTION

FUNCTION SetRotation: phi
self.phi = phi
ENDFUNCTION

FUNCTION IncRotation: value#
INC self.phi,value#
ENDFUNCTION

FUNCTION DecRotation: value#
DEC self.phi,value#
ENDFUNCTION

FUNCTION SetColor: _rgb
self.col = _rgb
ENDFUNCTION

FUNCTION SetHandle: x, y
self.hx = x
self.hy = y
ENDFUNCTION
ENDTYPE




LOCAL icon AS T2DEntity
LOCAL copy AS T2DEntity

icon.Load("icon.png")
icon.SetPosition(200,100)
icon.SetHandle( icon.Get_Size_X()/2, icon.GET_Size_Y()/2 )

icon.PushBack() // you can "pushback" into the T2DEntityArray[] to use all T2D's in a FOREACH-loop!



icon.CopyToEntity(copy)
icon.SetColor(RGB(0xff, 0x80, 0xff)) // set color after copy ;)


copy.SetPosition(200,300)

WHILE TRUE

icon.IncRotation(1)
icon.Draw()
PRINT icon.GetFilename$(), icon.Get_X(), icon.Get_Y()

copy.DecRotation(2)
copy.Draw()
PRINT copy.GetFilename$(), copy.Get_X(), copy.Get_Y()
SHOWSCREEN
WEND
END



//#################### THIS IS HOW TO USE THE TYPEARRAY ACCESS !! ################################

//WHILE TRUE
//
// // in this loop only one sprite is drawn, because only "icon" was called with PushBack!
// FOREACH t IN T2DEntityArray[]
// t.DecRotation(2)
// t.Draw()
// PRINT t.GetFilename$(), t.Get_X(), t.Get_Y()
// NEXT
//
//SHOWSCREEN
//WEND
//END

//################################################################################################



// this function is from AndyH !
// Hope its ok if i use it?!
// Let me know, if i do something wrong :)
@FUNCTION _RotoZoomPoly: _spr, _id, _x,_y, _angle, _scale, _col, _hx, _hy, _w, _h
LOCAL sinp, cosp, spw, sph, dx, dy
LOCAL hx, hy, dxp, dxm, dyp, dym

sinp = SIN(_angle)
cosp = COS(_angle)

spw = _w-0.5
sph = _h-0.5

dx=spw/2
dy=sph/2

hx = (dx - _hx)
hy = (dy - _hy)

dxp = (dx+hx) * _scale
dxm = (dx-hx) * _scale
dyp = (dy+hy) * _scale
dym = (dy-hy) * _scale

STARTPOLY _id
POLYVECTOR _x - cosp * dxm - sinp * dym, _y - cosp * dym + sinp * dxm, 0.5, 0.5, _col
POLYVECTOR _x - cosp * dxm + sinp * dyp, _y + cosp * dyp + sinp * dxm, 0.5, sph, _col
POLYVECTOR _x + cosp * dxp + sinp * dyp, _y + cosp * dyp - sinp * dxp, spw, sph, _col
POLYVECTOR _x + cosp * dxp - sinp * dym, _y - cosp * dym - sinp * dxp, spw, 0.5, _col
ENDPOLY

ENDFUNCTION


[attachment deleted by admin]
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

monono

I like it!. I am just exactly there as I want to clean up some code of mine.
Just the looping through T2DEntityArray[] doesn´t really works the way I expected. If you push an object into that array, you create a copy. So every change of - in you example - icon, has no effect on the object in the array. The same problem appears with the parent object of your code. I know it´s not finished, but how are you planning to use the parent object? It is just a capsulated object inside another object, so one obejct can´t be an object itself, maybe in T2DEntityArray[], and a parent object at the same time. You could make list of objects and in the creation process return an index instead of the typeobject itself. That would be sad, because the wonderfull function in types syntax would get lost. And u have to deal with a changed index after deleting an entry from the list.
If you know a way of just making an absolut handle/pointer to that object instead of copying it please let me know.

Schranz0r

The only way is to use INLINE-Code...  O_O
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Kitty Hello

yes. Please use an index system instead of real objects. It's easier to handle.

Schranz0r

hmm but i want to say -> pic.Draw() and not Draw(pic) !
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Kitty Hello

well, then you have to store a copy of the real object somewhere and such. Do as you want.
You cannot store a pointer/reference, though. You need a true copy.

monono

I hope your not too sad now Schranz0r!  :'(
Maybe Kitty writes it on the list for release 10 or so. It would really be a big improvement.

I do it with an index system as well. It truely has some flaws.
Other than the missing clean looking function-in-types thingy I ran into some other problems a few times.
If you delete an object, you cannot really DELETE it. That would mess up the hold index. You have to flag it as deleted and fill that position later with new objects. The problem that it is just not a pointer stays. If you want to proof the existence of an object, you never can be a 100% sure if it´s an old or a new object at that position. An idea is to give  objects an optional name while creation. That way you can proof it. 

If you still want to do your entitysystem I can show you mine. Still not public yet. It´s still a mess.