Generating images for combined substrate

posted in SnailLife
Published May 27, 2015
Advertisement
The Laravel 5 migration is complete and I'm back to substrate mixing and brain bug fixing!

Actually, I think now that basic mixing is done I'm going to do a few weeks of just bug fixing. The substrate has no effect on snails' attributes or behaviour yet, but it will. For now you can just mix different substrate items together into new substrate items.

I'm still not sure what the best way to create images for the custom-mixed substrate might be. The possibilities are pretty much endless - you can mix any substrate-type item with any other substrate-type item.

So what happens is - the user goes to their closet. They pick two substrate items to mix together. Then this whole mess happens:

protected function mixSubstrate() { $itemID1 = Input::get('itemID1'); $itemID2 = Input::get('itemID2'); $item1 = $this->findItem($itemID1); $item2 = $this->findItem($itemID2); $item1WeightG = $item1->template->weightG; $item2WeightG = $item2->template->weightG; $resultingItemName = Input::get('itemname'); $item1Attributes = $item1->template->nutrition->getAttributes(); $item2Attributes = $item2->template->nutrition->getAttributes(); $imageTitle = preg_replace('/\s+/', '', $resultingItemName) . '.png'; $fileToSaveTo = '/assets/img/items/terrain/custom/' . $imageTitle; $resultingImage = Utility::MergeTwoImages($item1->fullImagePath, $item2->fullImagePath, $fileToSaveTo); // Create resulting item template $resultingItemTemplate = new ItemUserTemplate(); $resultingItemTemplate->description = 'Custom item created by combining ' . $item1->template->name . ' and ' . $item2->template->name; $resultingItemTemplate->weightG = $item1WeightG + $item2WeightG; $resultingItemTemplate->name = $resultingItemName; $resultingItemTemplate->typeID = $item1->template->typeID; $resultingItemTemplate->ownerID = Auth::user()->userID; $resultingItemTemplate->imageSubdir = 'terrain/custom/'; $resultingItemTemplate->imageName = $imageTitle; $resultingItemTemplate->save(); // Create resulting item nutrition template $resultingItemNutrition = new ItemUserNutrition(); $resultingItemNutrition->templateID = $resultingItemTemplate->templateID; foreach ($item1Attributes as $key => $value) { if ($key !== 'templateID' && $key !== 'updated_at' && $key !== 'created_at') { $resultingItemNutrition->$key = $item1Attributes[$key] + $item2Attributes[$key]; } } $resultingItemNutrition->save(); // Create item of new template $resultingItemProperties = [ 'templateID' => $resultingItemTemplate->templateID, 'ownerID' => Auth::user()->userID, 'isUserTemplate' => 1 ]; $resultingItem = $this->createNewItem($resultingItemProperties); // Delete original items $item1->deleteItem(); $item2->deleteItem(); return Redirect::back()->with('resultingItem', $resultingItem);}
To actually create the image for the new item I added a new utility function:

public static function MergeTwoImages($imgPath1, $imgPath2, $fileToSaveTo = null) { $x = 50; $y = 50; $root = $_SERVER['DOCUMENT_ROOT']; $finalImage = imagecreatetruecolor($x, $y); imagesavealpha($finalImage, true); $sortedImages = [$imgPath1, $imgPath2]; sort($sortedImages); $image1Layer = imagecreatefrompng($root . $sortedImages[0]); $image2Layer = imagecreatefrompng($root . $sortedImages[1]); $opacity = 50; ImageCopyMerge($finalImage, $image1Layer, 0, 0, 0, 0, 50, 50, $opacity); ImageCopyMerge($finalImage, $image2Layer, 0, 0, 0, 0, 50, 50, $opacity); header('Content-Type: image/png'); if ($fileToSaveTo) { $fileToSaveTo = $root . $fileToSaveTo; imagepng($finalImage, $fileToSaveTo); } return $finalImage;}

The results look like the image next to this post (this example is experimenting with mixing 1kg oak leaves with 1kg sand).

It'll do for now. After some bug fixing I'll go back to substrate and start having it actually have an effect on the snails.
Previous Entry Migrating to Laravel 5
Next Entry Items
2 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement