Difference between revisions of "2D Space Shooter Part 4: Player"

From GiderosMobile
Line 2: Line 2:
 
It is time to make our ship controllable. Since only the player's ship will be controllable, let's make a subclass of our generic Ship to handle that. We will call it 'PlayerShip'.
 
It is time to make our ship controllable. Since only the player's ship will be controllable, let's make a subclass of our generic Ship to handle that. We will call it 'PlayerShip'.
  
Go ahead and create a new file named 'player.lua'. This new file will depend on the Ship class, so right click on the file name in gideros studio and access 'Code dependencies'. From there tell Gideros that player.lua will depend on ships.lua.
+
Go ahead and create a new file named '''player.lua''', this new file will depend on the Ship class. There are two methods to implement code dependency in Gideros Studio:
 +
# right click on the file name and access 'Code dependencies'. From there tell Gideros that player.lua will depend on ships.lua
 +
# by code by adding ''--!NEEDS:your_file.lua'' at the top of a file
  
 
== Player control ==
 
== Player control ==
The code if this file is rather simple: we register mouse events and react upon them:
+
The code of this file is rather simple: we register mouse events and react upon them:
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
PlayerShip=Core.class(Ship)
+
--!NEEDS:ships.lua
 +
PlayerShip = Core.class(Ship)
  
 
function PlayerShip:init()
 
function PlayerShip:init()
self:addEventListener(Event.MOUSE_DOWN,self.onMouseDown,self)
+
self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
self:addEventListener(Event.MOUSE_UP,self.onMouseUp,self)
+
self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
self:addEventListener(Event.MOUSE_MOVE,self.onMouseMove,self)
+
self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
self:addEventListener(Event.MOUSE_HOVER,self.onMouseMove,self)
+
self:addEventListener(Event.MOUSE_HOVER, self.onMouseMove, self)
 
end
 
end
  
 
function PlayerShip:destroy()
 
function PlayerShip:destroy()
self:removeEventListener(Event.MOUSE_DOWN,self.onMouseDown,self)
+
self:removeEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
self:removeEventListener(Event.MOUSE_UP,self.onMouseUp,self)
+
self:removeEventListener(Event.MOUSE_UP, self.onMouseUp, self)
self:removeEventListener(Event.MOUSE_MOVE,self.onMouseMove,self)
+
self:removeEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
self:removeEventListener(Event.MOUSE_HOVER,self.onMouseMove,self)
+
self:removeEventListener(Event.MOUSE_HOVER, self.onMouseMove, self)
-- Call the inherited destructor
+
-- call the inherited destructor
 
Ship.destroy(self)
 
Ship.destroy(self)
 
end
 
end
  
 
function PlayerShip:onMouseDown(event)
 
function PlayerShip:onMouseDown(event)
-- Start firing
+
self.fire=true -- start firing
self.fire=true
 
 
end
 
end
  
 
function PlayerShip:onMouseUp(event)
 
function PlayerShip:onMouseUp(event)
-- Stop firing
+
self.fire=false -- stop firing
self.fire=false
 
 
end
 
end
  
 
function PlayerShip:onMouseMove(event)
 
function PlayerShip:onMouseMove(event)
-- Clamp the position to something in the screen
+
-- clamp the player ship position to something in the screen
local x=(event.x<>(SCR_LEFT+64))><(SCR_RIGHT-64)
+
local x = (event.x<>(SCR_LEFT+64))><(SCR_RIGHT-64)
local y=(event.y<>(SCR_TOP+64))><(SCR_BOTTOM-64)
+
local y = (event.y<>(SCR_TOP+64))><(SCR_BOTTOM-64)
self:setPosition(x,y)
+
self:setPosition(x, y)
 
end
 
end
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 46: Line 47:
  
 
== Using our controllable ship ==
 
== Using our controllable ship ==
This is were OOP power shines. The only thing you need to do to make the ship you added to main.lua controllable is just to instanciate a PlayerShip instead of a generic Ship. In main.lua, change:
+
This is were OOP really shines. The only thing you need to do to make the ship you added to main.lua controllable is just to instanciate a PlayerShip instead of a generic Ship. In main.lua, change:
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
local player=Ship.new()
+
local player = Ship.new()
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
into:
 
into:
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
local player=PlayerShip.new()
+
local player = PlayerShip.new()
 
</syntaxhighlight>
 
</syntaxhighlight>
 
and you are done!
 
  
 
{{#widget:GApp|app=SpaceShooter_PL1.GApp|width=320|height=480|plugins=bump}}
 
{{#widget:GApp|app=SpaceShooter_PL1.GApp|width=320|height=480|plugins=bump}}

Revision as of 18:22, 15 November 2023

It is time to make our ship controllable. Since only the player's ship will be controllable, let's make a subclass of our generic Ship to handle that. We will call it 'PlayerShip'.

Go ahead and create a new file named player.lua, this new file will depend on the Ship class. There are two methods to implement code dependency in Gideros Studio:

  1. right click on the file name and access 'Code dependencies'. From there tell Gideros that player.lua will depend on ships.lua
  2. by code by adding --!NEEDS:your_file.lua at the top of a file

Player control

The code of this file is rather simple: we register mouse events and react upon them:

--!NEEDS:ships.lua
PlayerShip = Core.class(Ship)

function PlayerShip:init()
	self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
	self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
	self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
	self:addEventListener(Event.MOUSE_HOVER, self.onMouseMove, self)
end

function PlayerShip:destroy()
	self:removeEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
	self:removeEventListener(Event.MOUSE_UP, self.onMouseUp, self)
	self:removeEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
	self:removeEventListener(Event.MOUSE_HOVER, self.onMouseMove, self)
	-- call the inherited destructor
	Ship.destroy(self)
end

function PlayerShip:onMouseDown(event)
	self.fire=true -- start firing
end

function PlayerShip:onMouseUp(event)
	self.fire=false -- stop firing
end

function PlayerShip:onMouseMove(event)
	-- clamp the player ship position to something in the screen
	local x = (event.x<>(SCR_LEFT+64))><(SCR_RIGHT-64)
	local y = (event.y<>(SCR_TOP+64))><(SCR_BOTTOM-64)
	self:setPosition(x, y)
end

Notice how we explicitly call the Ship.destroy() function from our subclass. Also note another use of the <> et >< Gideros operators when clamping the player ship position on screen.

Using our controllable ship

This is were OOP really shines. The only thing you need to do to make the ship you added to main.lua controllable is just to instanciate a PlayerShip instead of a generic Ship. In main.lua, change:

local player = Ship.new()

into:

local player = PlayerShip.new()


Prev.: 2D_Space_Shooter_Part_3:_Ships
Next: 2D Space Shooter Part 5: Firing


Tutorial - Making a 2D space shooter game